Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoError: connection 0 to localhost:8081 closed at Function.MongoError.create #61

Open
mdmasumbillah opened this issue Jun 7, 2018 · 1 comment

Comments

@mdmasumbillah
Copy link

mdmasumbillah commented Jun 7, 2018

Hello bro, When I run "nodemon server.js" then show this error. I'm tried for the error

# 1. server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var passport = require('passport');
var ejs = require('ejs');
var session = require('express-session');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
var dbUrl = require('./config/db.js').url;

var option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000
};
// Connect to mongoose
mongoose.connect(dbUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Connected');
});

app.use(flash());
app.use(session({
secret: 'meanlocalauth'
}));
app.use(bodyParser.json);
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(passport.initialize());
app.use(passport.session());

require('./router/routes.js')(app);
// require('./passport.js')(app, passport);

app.use(morgan('dev'));
var PORT = process.env.PORT || 8081;
app.listen(PORT, function(){
console.log('Listening on port '+ PORT);
});

#2. db.js
module.exports = {
'url': 'mongodb://localhost:8081/loginapp'
}

# 3.package.json
{
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.18.3",
"connect-flash": "^0.1.1",
"cookie-parser": "^1.4.3",
"ejs": "^2.6.1",
"express": "^4.16.3",
"express-session": "^1.15.6",
"mongodb": "^3.0.10",
"mongoose": "^4.10.8",
"morgan": "^1.9.0",
"passport": "^0.4.0",
"passport-local": "^1.0.0"
},
"scripts": {
"start": "node server.js"
},
"name": "loginapp",
"version": "1.0.0",
"main": "server.js",
"devDependencies": {},
"author": "Masum",
"license": "MIT",
"description": ""
}

connection error: { MongoError: connection 0 to localhost:8081 closed
at Function.MongoError.create (/Users/ms/loginApp/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:29:11)
at Socket. (/Users/ms/loginApp/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:202:22)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at TCP._handle.close [as _onclose] (net.js:554:12)
name: 'MongoError',
message: 'connection 0 to localhost:8081 closed' }

@kishanjasani
Copy link

In the server.js

// Do have to require like this
var dbUrl = require('./config/db').url;

Not with .js extension

var dbUrl = require('./config/db.js').url;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants