直接上代码 server.js
var qs = require('querystring');
require('http').createServer(function (req, res) {
var body = '';
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
res.writeHead(200);
res.end('Done');
console.log('\n got name \033[90m' + qs.parse(body).name + '\033[39m\n');
});
}).listen(3000);
client.js
var http = require('http'),
qs = require('querystring');
function send (theName) {
http.request({
host : '127.0.0.1',
port : 3000,
url : '/',
method : 'POST'
}, function (res) {
res.setEncoding('utf-8');
res.on('end', function () {
console.log('\n \033[90m request complete!\033[39m');
process.stdout.write('\n your name: ');
});
}).end(qs.stringify({name : theName}));
}
process.stdout.write('\n your name: ');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (data) {
send(data.replace('\n',''));
});
问题:
按说在client
端出现your name:
的提示之后,我输入名字,按回车之后server
端就会出现your name ***
,而client
端将会出现request complete!
和your name:
的提示,但是为什么我的client
端就是不会出现这两句话啊?