Whether the end event of response in node.js must listen to the data event before emit can be emitted

  node.js, question
//server.js
 var qs = require('querystring');
 require('http').createServer(function(req, res){
 var body = '';
 req.on('data', function(chunk){
 Body plus = chunk;
 });
 req.on('end', function(){
 res.writeHead(200);
 res.end('Done');
 Log ('\ ngotname \ 033 [90m' plus qs.parse(body).name plus' \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('utf8');
 //res.on('data', function(){return ;  });
 res.on('end', function(){
 console.log('\n  \033[90m request complete!  \033[39m');
 process.stdout.write('\n  your name: ');
 });
 }).end(qs.stringify({name: theName}));
 bracket
 process.stdout.write('\n  your name: ');
 process.stdin.resume();
 process.stdin.setEncoding('utf-8');
 process.stdin.on('data', function(name){
 send(name.replace('\n', ''));
 });

-From “The Great Node. JS” P98-P99

  • The callback function in the end event will not be executed if the event monitoring data in client.js is watched out.

  1. Hope to be able to help explain, thank you.

  2. By the way, how should I solve this problem so that I can solve it myself next time, thank you.

End is an event that occurs after reading data.