javascript - nodejs execution client node client.js cannot connect and reports an error
PHP中文网
PHP中文网 2017-05-31 10:40:54
0
1
746

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中文网

认证0级讲师

reply all(1)
过去多啦不再A梦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template