scotch-io / easy-node-authentication

Code for the entire scotch.io tutorial series: Complete Guide to Node Authentication
https://scotch.io/tutorials/easy-node-authentication-setup-and-local
MIT License
1.36k stars 747 forks source link

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

Open mdmasumbillah opened 6 years ago

mdmasumbillah commented 6 years ago

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 commented 5 years ago

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;