新建一个js文件,写入一下代码
var http = require("http");
/*from www.w3cschool.cn*/
function process_request(req, res) {
var body = 'Thanks for calling!\n';
var content_length = body.length ;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
res.end(body);
}
var s = http.createServer(process_request);
//端口监听设置
s.listen(8080);
CLI运行js文件
node web.js
你的计算机现在有一个在端口8080上运行的Web服务器。我们可以在web浏览器中输入http://localhost:8080。
此时可以使用NODE内置CURl工具测试服务情况
curl -i http://localhost:888
返回服务状态信息
HTTP/1.1 200 OK
Content-Length: 20
Content-Type: text/plain
Date: Tue, 15 Feb 2013 03:05:08 GMT
Connection: keep-alive
Thanks for calling!