一个用 Node 编写的简单响应 'Hello World' 的Web 服务器示例:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
将以上代码保存到example.js
,运行以下命令来启动服务器:
> node example.js
Server running at http://127.0.0.1:8124/
文档中所有示例都能这样运行。