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

177
Views
Express JS : How to manage routing by headers request?

I have Managed an API service base on Express JS. I have 2 or 3 clients that request my API. All the requests from the client are handled by single monolythic Apps. Currently, I handle that request with this code:

const express = require('express');
const webRoute = require('./src/routes/web/index')
const cmsRoute = require('./src/routes/cms/index');

app.use('/web', webRoute)
app.use('/cms', cmsRoute)


// This Code works just fine. The routes are defined by URL request

But I want the route request not from Url, but requested by its Headers. It looks like this appKey = 'for-key' appName='web'

curl -X GET \
  http://localhost:3000/cms \
  -H 'Authorization: Bearer asfsfafgdgfdgddafaggafafasgfghhjhkgflkjkxjvkjzvxzvz' \
  -H 'Postman-Token: c36d6d5a-14b7-40bf-85e0-1bf255c815de' \
  -H 'appKey: keyloggers' \
  -H 'appName: web (i want by this header)' \
  -H 'cache-control: no-cache'

Thx for all.

edit notes:

In my current code, to call an API I am using this:

https://localhost:3000/cms/user/profile or https://localhost:3000/web/user/profile I want all requests to only use https://localhost:3000/user/profile without adding a prefix web or cms

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Based on a comment, it appears you want to use a single URL form such as:

https://localhost:3000/user/profile

And, have it routed to the correct router based on the appName custom header.

You can do that by just checking the custom header value and manually sending the request to the desired router.

const webRoute = require('./src/routes/web/index')
const cmsRoute = require('./src/routes/cms/index');

// custom middleware to select the desired router based on custom header
app.use((req, res, next) => {
    const appName = req.get("appName");
    if (appName === "web") {
        webRoute(req, res, next);
    } else if (appName === "cms") {
        cmsRoute(req, res, next);
    } else {
        next();
    }
});
about 4 years ago · Juan Pablo Isaza Report

0

You can use a default route, then based on request headers, you can redirect to your route.

//consider this a default route. You can arrange any in your program

router.get('/', function (req, res) {
  // switch on request header variable value
  
  switch (req.get("appName")) {
  case "web":
    res.redirect('/web')
    break;
  case "cms":
    res.redirect('/cms')
    break;
}
})

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!