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

321
Views
Express, how to set custom request headers on all routes for all requests?

I have this node API wrapper service, where I need to set REQUEST custom headers on all routes / endpoints at once instead of doing that on each request. I have tried the following in app.js but receive a not function error: req.set is not a function.

const customHeadersAppLevel = function (req, res, next) {
  req.set("User-Agent", "zendx/sop API");
  next();
};
app.all("*", customHeadersAppLevel);

Is there anyway we can do this on the app level or should I just go ahead with setting custom request headers on individual routes/requests, respectively.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Assuming that app = express() and we are at the top of the server's root file, you could set those headers for all your request like this:

const customHeadersAppLevel = function (req, res, next) {
  req.headers['User-Agent'] = 'zendx/sop API'
  next();
};

app.use(customHeadersAppLevel);
over 4 years ago · Santiago Trujillo Report

0

I think there is no set function in Request. Use headers property to set the request header.

 const customHeadersAppLevel = function (req, res, next) {
    req.headers['User-Agent'] = 'zendx/sop API';
    next();
 };
 app.all('*', customHeadersAppLevel);

You can also use Application-level middleware. Something like this,

const express = require('express')
const app = express()

app.use((req, res, next) => {
  req.headers['User-Agent'] = 'zendx/sop API';
  next()
})

// Other Route comes here
over 4 years ago · Santiago Trujillo 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!