Skip to content
Advertisement

Request interceptors not modifying the value of request header in node js

I am using http-proxy-middleware to create a proxy and it’s running successfully. Before calling app.use('/',proxy_options); I am trying to intercept my request and modifying the request header but updated value is not reflecting in headers.

app.use('/',(req,res,next)=>{
const token=getToken();
req.header['authorization']=token;
next();
});

Even I tried with req.header.authorization=token; and also without next();. When I am trying to print the my request header authorization:'' is coming as blank. Can any one let me know why this happening and how I can resolve this.

Any help or suggestions must be appreciated.

Advertisement

Answer

If your getToken() function is fetching token from other apis, then you should add await in front of it.

Try to use below code,

app.use('/', async (req,res,next)=>{
const token=await getToken();
req.headers['authorization']=token;
next();
});

You also need to replace header by headers, as mentioned above in code snippet.

It should work.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement