我是 node.js 的初学者,我在这里安装了它:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
我从控制台试了一下,它成功了:
console.log('Hello World!');
nodejs helloConsole.js
> Hello World!
然后我尝试在 HTTP Server 中实现它,这是代码:
var http = require("http");
http.createServer(function (request, response) {
request.on("end", function () {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
});
}).listen(8080);
我从终端运行它:
nodejs 你好.js
然后我在 http://localhost:8080/
打开浏览器,加载需要很长时间,然后在 chrome 上它给我:
无法加载网页,因为服务器没有发送数据。
firefox 给了我:
连接超时
注意:其他网络服务器工作正常,如 apache。
最佳答案
您正在监听请求参数的“结束”事件,但在您的最外层回调函数被调用时,请求已经结束,因此订阅该事件为时已晚。
你可以直接从最外层的回调中响应,nodejs.org中的示例代码就是这样显示的。
关于javascript - Node.js 无法加载网页 连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16394624/