我正在尝试从 localhost:8000 到 localhost:5000 为 url '/serverTest' 发出 GET 请求。我收到套接字挂起错误。我该如何解决?
端口 5000 上的服务器(将处理请求的服务器):
var express = require("express"), http = require('http');
app.get('/serverTest', function(req, res){
//authenticate request
//send message
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify({message:'hello'}));
});
app.listen(5000, function() {
console.log("Listening on " + port);
});
端口 8000 上的服务器(发出请求的服务器):
var express = require("express"), http = require('http');
var port = 8000;
app.listen(port, function() {
console.log("Listening on " + port);
makeACall();
});
function makeACall(){
var options = {
host:'localhost',
port: 5000,
path: '/serverTest',
method: 'GET'
};
http.request(options, function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
});
}
托管在端口 8000 的服务器获得的错误:
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1442:15)
at Socket.socketOnEnd [as onend] (http.js:1538:23)
at Socket.g (events.js:175:14)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)