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.
Is the server still up? Make sure
node server.js
does not end, reopen a shell and runnode client.js