Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

409
Views
NodeJS Mongoose handle database connection failed exception

I'm building a restful web service api using NodeJS.It uses Mongoose as ODM and using MongoDB for backend. Below i will explain my scenario

I started nodejs server After that i shutdown the MongoDB database. Then call the GET api call,it doest catch any errors and api call get hang.

database config in main.js file

var mongoose = require('mongoose');
var uri = 'mongodb://localhost/mydb';
mongoose.Promise = global.Promise;
var options = { server: {socketOptions: { keepAlive: 300000, connectTimeoutMS: 10000 } } } ;
mongoose.connect(uri,options);
var db = mongoose.connection;
db.on('error',console.log.bind(console,'connection refused !!!!!'));
db.once('open', console.log.bind(console,'connection success !!!!!'));

this is my basic GET call

 var express = require('express');
    var router = express.Router();
    var mongoose = require('mongoose');
    var rootRes = require('../model/rootresources.js');

    router.get('/', function(req, res, next) {
      rootRes.find({},function (err, rootResource) {
    if (err){
     console.log('Error occurd !!!') }
    res.json(rootResource);
});
});

Even database connection failed, the code does not goes to error block. So didn't capture the database refuse when database connection is failed in the API call.

I want to capture that error and send internal server error (code:500) to client. I tried to find the solution but still could not find it Any solutions or do i made a mistake ?

Thank you Amila

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Did you put the two parts of code in the same file(ie. main.js) or two different files.

put them in the same file, and run node main.js do throw exceptions.

// main.js
var mongoose = require('mongoose');
var uri = 'mongodb://localhost/mydb';
mongoose.Promise = global.Promise;
var options = { server: {socketOptions: { keepAlive: 300000, 
connectTimeoutMS: 10000 } } } ;
mongoose.connect(uri,options);
var db = mongoose.connection;
db.on('error',console.log.bind(console,'connection refused !!!!!'));
db.once('open', console.log.bind(console,'connection success !!!!!'));

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var rootRes = require('../model/rootresources.js');

router.get('/', function(req, res, next) {
  rootRes.find({},function (err, rootResource) {
  if (err){
    console.log('Error occurd !!!') }
  res.json(rootResource);
  });
});

exceptions are:

connection refused !!!!! { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
etc...

So, I think maybe you put codes about express in a file like index.js and codes about mongoose connection in another file. And just run node index.js in command line. While running codes in index.js will not include codes in other files, codes in main.js will not be executed. As the result, there is no error info.

Updates Two ways of I know two ways of doing this:

1.In main.js create function which creates connection to database and returns a instance of db so that you can call it function in you main code.

// main.js like this
var mongoose = require('mongoose');
function createConnection(url) {
  mongoose.connect(url,options);
  var db = mongoose.connection;
  db.on('error',console.log.bind(console,'refused !!!!!'));
  db.once('open', console.log.bind(console,'success !!!!!'));
  return db;
}
// export function
module.exports = createConnection;

// in your index.js 
var createConnection = require('./main.js');
var db = createConnection(url);
// other codes here

2.Using require or vm to compile and run javascipt code. You can find vm api detail here

//main.js
var mongoose = require('mongoose');
var uri = 'mongodb://localhost/mydb';
mongoose.Promise = global.Promise;
var options = { server: {socketOptions: { keepAlive: 300000, 
connectTimeoutMS: 10000 } } } ;
mongoose.connect(uri,options);
var db = mongoose.connection;
db.on('error',console.log.bind(console,'connection refused !!!!!'));
db.once('open', console.log.bind(console,'connection success !!!!!'));

// index.js
// require will load file and execute automaticly
var scriptSrc = require('./main');
// other codes here

You can think of the second way as using eval('var mongoose = require('mongoose'); var uri = 'mongodb://localhost/mydb'; etc...)

over 4 years ago · Santiago Trujillo Report

0

mongoose connection do not happen unless you hit a request. so its best you handle it in your first request middleware. see code insight bellow.

module.exports = function () {

return function (req, res, next) {

            mongoose.connect(URL, MONGO_OPTIONS);
            mongoose.connection
                .once('open', () => { })
                .on('error', (error) => {
                     res.status(401).json({});
                });
...

Then pass the middleware above to your router: let me know if you need more explanation

router.get('/', myMiddleware(){})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!