我正在尝试使用http模块创建一个简单的服务器和一个带有node.js的简单客户端.服务器工作正常,但客户端不工作.请帮我找一下这个bug ...
服务器是:
var server = require('http').createServer();
server.on('request', function(req, res){
res.end("hello, world");
});
server.listen(4000);
Run Code Online (Sandbox Code Playgroud)
客户是:
var options = {
host : 'localhost',
port : 4000,
method : 'GET',
path " '/'
};
require('http').request(options, function(res){
console.log(require('util').inspect(res));
res.on('data', function(data){
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
我在不同的终端窗口中运行它们作为节点server.js和节点client.js.
我在大约10分钟后在client.js运行终端上得到以下提到的错误.
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1473:15)
at Socket.socketOnEnd [as onend] (http.js:1569:23)
at Socket.g (events.js:175:14)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Run Code Online (Sandbox Code Playgroud)
谢谢 !
request()HTTP库的方法不会自动结束请求,因此它们保持打开状态并超时.相反,您应该使用req.end()或使用get()方法结束请求,这将自动执行此操作.
var http = require('http');
var req = http.request(options, function(res) {
// handle the resposne
});
req.end();
Run Code Online (Sandbox Code Playgroud)
要么:
http.get(options, function(res) {
// handle the resposne
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4332 次 |
| 最近记录: |