server.js
var http = require('http');
var server = http.createServer(function(req,res){
if(req.url !== '/favicon.ico'){
req.on('data',function(data){
console.log('服务器端接收到数据:' + data);
res.write('确认数据:' + data);
})
req.on('end',function(){
res.end();
})
}
}).listen(1337,'127.0.0.1');
client.js
var http = require('http');
var options = {
hostname : 'localhost',
port : 1337,
path : '/',
method : 'POST'
}
var req = http.request(options,function(res){
res.on('data',function(chunk){
console.log('客户端接收到数据:' + chunk);
})
});
req.write('你好');
req.end('再见');
执行结果: