search

Home  >  Q&A  >  body text

javascript - nodejs execution client node client.js cannot connect and reports an error

nodejs reports an error when executing client node client.js.
All steps are based on the novice nodejs tutorial.
1. Create HTTP server architecture (using port 8081) and create server.js.
2. Create an index.htm file.
3.node server.js, no problem

4. Create a Web client and create the client.js file!

var http = require('http');
// 用于请求的选项
var options = {
   host: 'localhost',
   port: '8081',
   path: '/index.htm'  
};
// 处理响应的回调函数
var callback = function(response){
   // 不断更新数据
   var body = '';
   response.on('data', function(data) {
   body += data;
   });
   response.on('end', function() {
      // 数据接收完成
  console.log(body);
   });
}
// 向服务端发送请求
var req = http.request(options, callback);
req.end();

5. Execute the client.js file and report an error. . The above code is based on the novice tutorial.

PHP中文网PHP中文网2724 days ago740

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-31 10:42:54

    Is the server still up? Make sure node server.js does not end, reopen a shell and run node client.js

    reply
    0
  • Cancelreply