gpt4 book ai didi

javascript - Node.js 服务器无法在 Windows 7 64 位上启动

转载 作者:行者123 更新时间:2023-11-28 19:42:36 25 4
gpt4 key购买 nike

看来我的设置有问题,我目前在 Windows 7 64 位计算机上安装了 node -v//v0.10.29

// Include http module.
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);

这是我通过 node http.js 运行的文件。除了这个文件之外,文件夹中没有其他任何东西。

运行这个小坏 child :node-inspector & node --debug http.js 在 cmd 中返回以下内容:

Node Inspector v0.7.4
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

我可以导航到 http://127.0.0.1:8080 以及建议的网址,使用 debug?etc 导航会给我一个控制台,类似于 Chrome 的开发人员工具。

导航到不带参数的网址会导致 Cannot GET/ 请求 404'in

使用上述标题:

Remote Address:127.0.0.1:8080
Request URL:http://127.0.0.1:8080/
Request Method:GET
Status Code:404 Not Found
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:127.0.0.1:8080
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Chrome/35.0.1916.153 Safari/537.36
Response Headersview source
Connection:keep-alive
Content-Type:text/html
Date:Thu, 17 Jul 2014 23:07:13 GMT
Transfer-Encoding:chunked
X-Powered-By:Express

那么我如何连接到 node.jswindows 7 中的简单 http 服务器

最佳答案

// Include http module.
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);

为了理解这个问题,我们可以做一些实验。

添加有关响应和请求结束事件的控制台日志

console.log("response start");
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
console.log("response end");
request.on("end", function () {
console.log("request end");
});

输出:

   response start
response end
request end

然后注释掉这行response.end。

console.log("response start");
response.writeHead(200, {'Content-Type': 'text/plain'});
//response.end('Hello World\n');
console.log("response end");
request.on("end", function () {
console.log("request end");
});

现在请求被卡住了。永远不会发出结束事件。

response start
response end

所以结论是请求的结束事件只有在response.end完成后才会被触发。您不应将响应放在请求的结束事件正文中

关于javascript - Node.js 服务器无法在 Windows 7 64 位上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24814969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com