I am trying to get MongoDB to work with Express.js. I have followed this article and created a file db/index.js
that provides functions for creating a database connection and returning the connection instance. In app.js
I require that file first, then call its function connect
, which successfully connects to the database.
However, when I require the file db/index.js
later in another file routes/users.js
, the database reference it returns is null
, although I expected it to be the same reference that I got when I connected in app.js
. Doesn't require
always return the same instance of the module that is returned on the first call to require
? What am I doing wrong?
EDIT: I understand that because the call db.connect
is asynchronous, the value of db
in users.js
might in theory be null
before the connecting function returns. However, I have verified via testing that even if connection is successfully created, db
is still null
when I do a POST request to path /users.
Here are the 3 relevant files.
app.js (execution starts here)
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
// configuration environment based on process.env.NODE_ENV environment variable
var config = require('./config');
var db = require('./db');
var app = express();
var routes = require('./routes/index');
var users = require('./routes/users');
app.use('/', routes);
app.use('/users', users);
// Connect to Mongo on start
db.connect(config.dbUrl, function (err) {
if (err) {
console.log('Unable to connect to ' + config.dbUrl);
process.exit(1);
} else {
console.log('Connected to ' + config.dbUrl);
}
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/tests', express.static(__dirname + '/test'));
app.use('/lib/jasmine-core', express.static(__dirname + '/node_modules/jasmine-core/lib/jasmine-core'));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
db/index.js
var MongoClient = require('mongodb').MongoClient;
var state = {
db: null
};
exports.connect = function (url, done) {
if (state.db)
return done();
MongoClient.connect(url, function (err, db) {
if (err)
return done(err);
state.db = db;
done();
})
}
exports.get = function () {
return state.db;
}
exports.close = function (done) {
console.log('db close');
if (state.db) {
state.db.close(function (err, result) {
state.db = null;
done(err);
})
}
}
exports.createIndexes = function () {
if (state.db) {
state.db.collection('events').createIndex(
{'id': 1},
{unique: true});
}
}
routes/users.js
var express = require('express');
var router = express.Router();
var db = require('../db').get();
router.post('/', function (req, res, next) {
//
// Problem: Every time this function is called, db is null!
//
var users = db.collection('users');
var userToAdd = req.body;
users.findOne({username: userToAdd.username}).then(function (foundUser) {
if (foundUser.length === 0) {
res.status(400).json({error: 'Username is already in use'});
} else {
// TODO: ADD USER
res.json(foundUser);
}
})
.catch(function (err) {
console.log(err);
res.status(500).json({error: 'Database error.'});
});
});
module.exports = router;