苏幕遮零

好好学习,天天向上!

导航

Node.js Hello World HTTP服务器

下面是个简单的HTTP服务器实现,它会用“Hello World”响应所有请求:

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'COntent-Type': 'text/plain'});
    res.end('Hello World\n');
}).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\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');

 

posted on 2017-08-31 16:26  苏幕遮零  阅读(226)  评论(0编辑  收藏  举报