【发布时间】:2014-12-30 03:02:15
【问题描述】:
我一直在使用 Node.js 中的 net 模块编写自己的静态 http 服务器。 这是我的代码:
var options = {
hostname: 'localhost',
port: 8000,
path: '/read.txt',
httpVersion : 1.1,
method: 'GET',
headers:{
'Content-Type':'text/plain',
'connection' : 'keep-alive'
}
};
function test(){
var sev = server.start(8000,"./folder",function(e){
e?(console.log(e)):(console.log("server is connected in port 8888"));
});
var req = http.request(options, function(res) {});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
server.stop(sev,function(e){e?(console.log(e)):(console.log("server is closed"));})
}
test()
这是 server 模块中的 stop 函数:
exports.stop = function(serverID,callback){
serverID.close();
callback();
}
但是当我运行它时,它会显示以下消息: 请求问题:连接 ECONNREFUSED。 找不到问题,会不会是node版本的问题?
【问题讨论】:
-
看起来 'server.stop' 在 'server.start' 有机会完成之前运行(或在
server.start完成之前运行http.request)。你的test方法正在运行,没有机会让每个都完成。
标签: javascript node.js http module