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

393
Views
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 answers
Answer question

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 Report
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!