[英]Node.js server won't start on windows 7 64-bit
看來我的設置有什么問題,我目前在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
運行的文件。 文件夾中除此文件外沒有其他任何內容。
運行這個小家伙: 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
,建議的url(使用debug?etc
導航)為我提供了一個控制台,類似於chrome的開發人員工具。
導航到沒有參數的url給我Cannot GET /
with request 404
具有上述標題:
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.js
和windows 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完成后才觸發請求的結束事件。 您不應將響應放在請求的結束事件正文中 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.