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

176
Vistas
How can I dynamically assign a database path in Express.js based on login?

I have a backend service that I would like to use as a single point of entry for my web application, and dynamically assign a database path based on the user login. I realize that this is not a scalable solution. I intend to use it during a testing period with several clients (accessing the ALPHA database), and also setting up a demo (accessing the SAND database). I have the following module that I have written as a simple test to see if the login is for the demo user, all other logins will go to the other resource:

config.js

var express         = require('express');
var app             = express();

module.exports.dbPath = function  (login){
    console.log('login - ', login);
    if (login === 'demo@mysite.com'){
        return process.env.DB_SAND;
    } else {
        return process.env.DB_ALPHA;
    }
};

My question is, how can I manage each unique login and assign a globally accessible reference for that session to direct each user session consistently to the correct database? Am I overcomplicating this? If there is a different approach that would be a better practice I would welcome a suggestion in another direction.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I would use it as a middleware, and attach it to the req object for each user, something similar this:

module.exports = {
    dbPath: function(req, res, next){
       var login = req.body.login;
       console.log('login - ', login);
       if (login === 'demo@mysite.com'){
          req.dbPath = 'DB_SAND';
       } else {
          req.dbPath = 'DB_ALPHA';
       }
       next();
    }
};
about 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