Node.js在本地主机上挂起/加载

我跟随这个初学者node.js教程( http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb ),我刚刚创build了我的第一个服务器使用此代码:

var http = require("http") http.createServer( function(request, response){ response.writeHead(200, {"Content-Type":"text/plain"}) response.write("hello world") response.end } ).listen(3333) 

这个工程很好,但是当我去到url localhost:3333 /我很短暂地看到“hello world”这个词,然后它就消失了。

快速观看此藤蔓video: https : //vine.co/v/MBJrpBEQvLX

有任何想法吗?

您忘记在response.end()的末尾加上括号。

代码应为:

 var http = require("http"); http.createServer( function(request, response){ response.writeHead(200, {"Content-Type":"text/plain"}); response.write("hello world"); response.end(); }).listen(3333); 

把你的Hello World放在响应#结束() 。 我也build议你阅读NodeJS API

 http.createServer(function (req, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(3333);