我正在尝试在本地主机上运行应用程序,但不断收到以下错误:
Error: connect ECONNREFUSED
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
我安装了node.js并执行了cmd命令:npm install和node index。我究竟做错了什么?我的代码如下索引文件:
var config = require('./config');
// ******************* WebSocket server *******************
var wsClients = {};
var WebSocket = require('ws');
var webSocketServer = new WebSocket.Server({ 'host': config.webSocketHost,'port': config.webSocketPort }, function(){
console.log('WebSocket listening '+config.webSocketHost+' on port '+config.webSocketPort+'.');
});
webSocketServer.on('connection', function(cws) {
var id = Math.random();
wsClients[id] = cws;
console.log("New connection " + id);
cws.on('close', function() {
console.log('Connection close ' + id);
delete wsClients[id];
});
cws.on('error', function() {
console.log('Connection error ' + id);
delete wsClients[id];
});
});
webSocketServer.on('message', function(data){
console.log(data);
});
// ******************* Web server *******************
var express = require('express');
var app = express();
app.use(express.static('public'));
app.set('views', './views');
app.set('view engine', 'ejs');
app.disable('x-powered-by');
app.get('/', function (req, res) {
res.render('index');
});
app.use(function(req, res, next) {
res.status(404).send('Sorry cant find that!');
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
var host = config.webHost || process.env.HOST || "127.0.0.1";
var port = config.webPort || process.env.PORT || 80;
app.listen(port, host, function () {
console.log('WebServer listening '+config.webHost+' on port '+config.webPort+'.');
});
// ******************* Twitter bot *******************
var Twitter = require('twitter');
var client = new Twitter(config);
var stream = client.stream('user');
stream.on('data', function(event, webSocketServer) {
for (var key in wsClients) {
wsClients[key].send(JSON.stringify(event));
}
});
stream.on('error', function(error) {
throw error;
});
答案 0 :(得分:1)
ECONNREFUSED
表示'服务器拒绝连接'。这可能是由于多种原因造成的。
以调试模式运行服务器,在其上设置断点并检查连接请求是否到达服务器应用程序。
如果连接请求未到达服务器应用程序,请检查本地防火墙,网络等。
如果它到达服务器应用程序并命中断点,请逐步运行并找到实际关闭连接的代码行。