1 回答

TA貢獻1827條經(jīng)驗 獲得超4個贊
// client.js
var http = require('http'),
qs = require('querystring');
var options = {
host: '127.0.0.1',
port: 3000,
url: '/',
method: 'post'
};
var send = (theName) => {
http
.request(options, (res) => {
res.setEncoding('utf8');
//這里非要寫一個on data不可,否則end事件不觸發(fā)
res.on('data', (chunk) => {
});
res.on('end', () => {
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('utf8');
process.stdin.on('data', (name) => {
send(name.replace('\r\n', ''));
});
文檔里有這么一句話:
你要不“消費”data
事件,就別想用end
,對此我也略蒙逼,不過這是事實,你就接受吧
添加回答
舉報