Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJS: Append header to incoming request object

I'm trying to append additional header to an incoming request, it can't seem to work.

server.get('/', function md1(req, res, next) {
    req.setHeader('px-test-header', 1234); // Error - "req.setHeader is not a function"
    req.headers['px-test-header'] = 1234; // nothing happens
 }, function (req, res, next) {
    console.log(req.get('px-test-header')); // always undefined
}, handler);

What am I doing wrong? Is it even possible?

Note - I do not want to modify the request object with additional parameter instead.

like image 487
Ben Diamant Avatar asked Jul 15 '26 23:07

Ben Diamant


1 Answers

setHeader is a function for response type of objects not requests as from the documentation

But if you still want to set the headers in request then you could do something like

app.get('/', function(req,res){
    req.headers.abc ='xyz'; 
    console.log(req);
});

req are stored in req.headers so you could add your custom headers here for application middle wares to use later.

like image 89
Vidur Khanna Avatar answered Jul 18 '26 14:07

Vidur Khanna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!