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

396
Vistas
Making a conditional app.use() with Node and Express?

I would like to be able to set an app.use() path depending on the domain my Node.JS server receives the request as to return one set of files or another. I have tried with the following code, but when testing the files are never returned to the client.

app.use('/scripts', (req, res) => {

    if (req.host == `mysite.com`) {

        express.static(path.resolve(__dirname, 'landing', 'frontend/scripts'));
        
    } else if (req.host == `admin.mysite.com`) {

        express.static(path.resolve(__dirname, 'admin', 'frontend/scripts'));
    }
});

I am using express as a dependancy to try and do this, but no avail, I am willing to try other packages if this can help solve my issue.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Not tested, but I would assume you can keep a reference to each static route and then just forward the requests, don't forget next so that normal 404 can be handled.

eg.

const static1 = express.static(path.resolve(__dirname, 'landing', 'frontend/scripts'));
const static2 = express.static(path.resolve(__dirname, 'admin', 'frontend/scripts'));

app.use('/scripts', (req, res, next) => {
    if (req.hostname == `mysite.com`) {
        static1(req, res, next);       
    } else if (req.hostname == `admin.mysite.com`) {
        static2(req, res, next);
    } else res.end(`host: ${req.hostname} not found`);
});
about 4 years ago · Juan Pablo Isaza 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