Node.js在localhost上保持挂起/加载

时间:2014-01-22 23:11:14

标签: javascript node.js localhost

我正在关注这个初学者node.js教程(http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb),我刚刚用这段代码创建了我的第一台服务器:

var http = require("http")

http.createServer(

    function(request, response){

        response.writeHead(200, {"Content-Type":"text/plain"})
        response.write("hello world")
        response.end

    }


).listen(3333)

这很好用,但是当我去网址localhost:3333 /我非常简短地看到“你好世界”这个词然后它就消失了。

请参阅此藤蔓以获取快速视频:https://vine.co/v/MBJrpBEQvLX

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

将您的Hello World放入 response#end() 。我还建议您阅读NodeJS API

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

答案 1 :(得分:0)

您忘记将括号放在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);