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

454
Views
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
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!