简体   繁体   English

尝试MongoDB连接时NodeJS断开连接

[英]NodeJS disconnecting when attempt MongoDB connection

In my app.js file, I have the following code 在我的app.js文件中,我有以下代码

var express = require('express');
var app = express();
var port = 8080;
var util = require('util');
var router = require('./base/js/routes.js');
//==================================================================
app.use('/', router);

// start the server
app.listen(port, function(request, response) {
    console.log('Port 8080: Server Begins');
});
//==================================================================
var ipaddress = '123.456.789';
//==================================================================
var mongoose = require('mongoose');

var mongoURI = "mongodb://"+ ipaddress +":27017/test";
var MongoDB = mongoose.connect(mongoURI);
MongoDB.on('error', function(err) {
    console.log(err.message);
});
MongoDB.once('open', function() {
    console.log("mongodb connection open");
});
//==================================================================

The line var MongoDB = mongoose.connect(mongoURI); 该行var MongoDB = mongoose.connect(mongoURI); is causing nodeJS not to work. 导致nodeJS无法正常工作。 I do not know why. 我不知道为什么。 NodeJS is on port 8080 and MongoDB is on port 27017. NodeJS位于端口8080上,而MongoDB位于端口27017上。

I am fairly certain I installed mongodb package (and opened the port correctly). 我相当确定我安装了mongodb软件包(并正确打开了端口)。 I just do not understand why nodeJS doesnt work when i include that connection line. 我只是不明白为什么当我包括该连接线时,nodeJS无法正常工作。

Side Note: Also I have the package forever installed: forever start -c nodemon app.js for nodeJS. 旁注 :另外,我还安装了永久安装的软件包: forever start -c nodemon app.js for nodeJS。 If that is any relevance. 如果有关系的话。

You are using wrong IP address format. 您使用的IP地址格式错误。

First try to connect with your local mongoDB instance if it work then you to check the IP address your trying to connect is correct or not. 首先尝试连接本地mongoDB实例(如果它可以正常工作),然后检查尝试连接的IP地址是否正确。

Add the correct error message if problem still remain same. 如果问题仍然存在,请添加正确的错误消息。

change your mongod.conf file from /etc folder 从/ etc文件夹更改mongod.conf文件

In mongod.conf you need to change bindIp 在mongod.conf中,您需要更改bindIp

If connection is local then set bindIp as 如果连接是本地的,则将bindIp设置为

bindIp = 127.0.0.1

and if you want to use remote database then change bindIp as 如果要使用远程数据库,则将bindIp更改为

bindIp = 0.0.0.0

then restart mongo service hope this helps... 然后重启mongo服务,希望对您有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM