一个简单的node.js服务

时间:2023-03-08 17:48:35
var http = require('http');
var qs = require('querystring'); var server = http.createServer(function(req, res) {
console.log('url:', req.url);
console.log('method:', req.method);
console.log('headers:', req.headers); if(req.method.toUpperCase() === 'POST') {
var postData = '';
req.on('data', function(data) {
postData += data;
});
req.on('end', function() {
console.log(qs.parse(postData));
});
} var ret = {
name: 'xiaoyu'
};
res.end(JSON.stringify(ret));
});
server.listen(3000); console.log('server runing at port:' + 3000);

log信息,主要是为了查看请求信息。