Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 669
Zeusox Avatar asked Aug 31 '25 11:08

Zeusox


1 Answers

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);
like image 159
yousoumar Avatar answered Sep 03 '25 00:09

yousoumar