在nodeJs中拒绝连接

时间:2015-11-27 12:26:11

标签: node.js mongodb

我尝试使用node.js将MongoDB连接到我的聊天应用程序。我有谷歌和阅读很多文章,但我无法弄清楚。 它的反感如下:

{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }

关注我的代码:

    var app = require('express')();
var http = require('http').Server(app);
var https = require('http')
var io = require('socket.io')(http);
var sockets = new Array;
var clients = new Array;
var webSockets = {}; // userID: webSocket
var userlist = {};
var allClients = [];

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/products';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
    if (err) {
        console.log(err);
    } else {
        //HURRAY!! We are connected. :)
        console.log('Connection established to', url);

        //Close connection
        db.close();
    }
});

1 个答案:

答案 0 :(得分:1)

我遇到了这个问题,事实证明我必须用127.0.0.1替换localhost并且它有效