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

457
Vistas
Subdomain routing node.js and express

I am trying to make a site where the code checks what subdomain the user is accessing from. I can get the host by running this:

app.get('/', (req, res) => {
    const host = req.get('host');
    const origin = req.get('origin');
    res.send(host)
})

And i get the result: localhost:5000.

But i want to make it so that if the user is accessing from domain1.com it uses one router, and if it is from domain2.com it uses another. here is the code im trying to use:

server.js

const express = require('express')
const app = express()
const router = express.Router();


app.set('view engine', 'ejs')


app.get('/', (req, res) => {
    const host = req.get('host');
    const origin = req.get('origin');
    if(host === "domain1.com:5000"){
        const test = require('./routes/test.js')
        app.use('/', test)
    }else if(host === "domain2.com:5000"){
        res.send("Domain2")
    }else{
        res.send(host)
    }
})

app.listen(5000)

/routes/test.js


const express = require('express')
const router = express.Router()

router.get('/', async (req, res) => {
    res.send("it Works")
})



module.exports = router

When i try accessing the domain1 it does not work, and i don't get any errors in console

about 4 years ago · Juan Pablo Isaza
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