下面是个简单的HTTP服务器实现,它会用“Hello World”响应所有请求:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'COntent-Type': 'text/plain'}); res.end('Hello World '); }).listen(3000); console.log('Server running at http://localhost:3000/');
下面是服务器的另一种写法,这样看起来响应request事件更明显:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'COntent-Type': 'text/plain'}); res.end('Hello World '); }).listen(3000); console.log('Server running at http://localhost:3000/');