【问题标题】:How to build a web application node.js [closed]如何构建 Web 应用程序 node.js [关闭]
【发布时间】:2014-06-15 09:35:01
【问题描述】:

我正在使用 node.js 构建一个 Web 应用程序。我有一个带有主链接的第一个 .js。当你点击这个链接时,我想去第二个 js。这第二个 js 是一个带有头部和主体的 html。在正文中,我只显示一个标题。 当我单击第一个 js 的链接时,没有任何反应,我没有错误但我没有看到第二个 js。

我只执行了我的第二个 js,它是正确的。在html中我没有错误。

我想要一些关于我可以从另一个调用 js 的示例或教程。

附上我的两个js

主要js:

var http = require("http");
var server = http.createServer(function(req, res) {
res.write('<html xmlns="http://www.w3.org/1999/xhtml">');
var linea6 = '<html>';
res.write(linea6);
var linea7 = '<head>';
res.write(linea7);
res.write('<meta content="utf-8" http-equiv="encoding">');
res.write('<meta http-equiv="content-type" content="text/html; charset=utf-8" />');
var linea7b = '<script type="text/javascript">';
res.write(linea7b);
var linea7c = 'var a = document.getElementById("totalbicis");';
res.write(linea7c);
var linea7d = '</script>';
res.write(linea7d);
var linea7e = '<body>';
res.write(linea7e);
var linea7f = '<a id="totalbicis" href="/pruebaformulario11.js">Total Bicis</a>';
res.write(linea7f);
var linea7g = '</body>';
res.write(linea7g);
var linea7h = '</html>';
res.write(linea7h);
res.end();
});
server.listen(7000);

第二个js

var http = require("http");
var server = http.createServer(function(req, res) {
console.log('en 11');
res.write('<html xmlns="http://www.w3.org/1999/xhtml">');
res.write('<html>');
res.write('<head>');
res.write('<meta content="utf-8" http-equiv="encoding">');
res.write('<meta http-equiv="content-type" content="text/html; charset=utf-8" />');
res.write('<script type="text/javascript">');
res.write('</script>');
res.write('</head>');
res.write('<body>');
res.write('Segunda pagina');
res.write('</body>');
res.write('</html>');
res.end();
});
server.listen(7000);

我需要这方面的信息。

有人可以帮我吗?

【问题讨论】:

  • 天哪。使用模板引擎!并且不要创建 2 个服务器!使用快递!
  • 我建议通读The Node Beginnera similar guide。 Node 中的 http.Server 代表整个 Web 应用程序,所有 URL 路径,来自根。对http.createServer() 的回调应检查req.methodreq.url 并根据它们决定如何响应所有组合,包括错误(例如404 Not Found)。

标签: javascript html node.js


【解决方案1】:

您不能让两个应用程序在同一个端口上侦听。尝试阅读如何在 Node.js 中创建多页应用程序。从有关路由的教程开始,从那里开始学习。

您永远不必为这样的应用程序创建多个服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 2012-12-24
    • 2015-12-10
    • 1970-01-01
    • 2010-11-25
    • 2011-04-18
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多