Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

412
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda