简体   繁体   中英

NodeJS disconnecting when attempt MongoDB connection

In my app.js file, I have the following code

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); is causing nodeJS not to work. I do not know why. NodeJS is on port 8080 and MongoDB is on port 27017.

I am fairly certain I installed mongodb package (and opened the port correctly). I just do not understand why nodeJS doesnt work when i include that connection line.

Side Note: Also I have the package forever installed: forever start -c nodemon app.js for nodeJS. If that is any relevance.

You are using wrong IP address format.

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.

Add the correct error message if problem still remain same.

change your mongod.conf file from /etc folder

In mongod.conf you need to change bindIp

If connection is local then set bindIp as

bindIp = 127.0.0.1

and if you want to use remote database then change bindIp as

bindIp = 0.0.0.0

then restart mongo service hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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