HTTP服务器不会侦听端口8000

时间:2014-07-12 13:39:13

标签: node.js

我是节点js和编程的新手。我已经安装了节点js并尝试在.js文件中运行以下代码:

JS

var http = require('http');
var server = http.createServer( function(request, response) {
    response.writeHead(200, {'Content-type': 'text/plain'});
    response.end('Hello World!');
    server.listen(8000);
    console.log('Server running on port 8000');
});

接下来,我在终端上运行命令节点helloworld.js。但是,当我试图打开时

http://localhost:8000

在chrome上,它说google chrome无法连接到“localhost:8000”。问题是什么?

1 个答案:

答案 0 :(得分:2)

您似乎错误地输入了示例代码。

var http = require('http');
var server = http.createServer(function(request, response) {
    response.writeHead(200, {'Content-type': 'text/plain'});
    response.end('Hello World!');
}); // <-----------
server.listen(8000);
console.log('Server running on port 8000');