请求拦截器不修改节点js中请求header的值
Request interceptors not modifying the value of request header in node js
我正在使用 http-proxy-middleware
创建代理,并且 运行 成功。
致电前 app.use('/',proxy_options);
我正在尝试拦截我的请求并修改请求 header 但更新后的值未反映在 headers.
中
app.use('/',(req,res,next)=>{
const token=getToken();
req.header['authorization']=token;
next();
});
我什至尝试过使用 req.header.authorization=token;
和不使用 next();
。
当我尝试打印时,我的请求 header authorization:''
显示为空白。
谁能告诉我为什么会这样以及我该如何解决这个问题。
必须感谢任何帮助或建议。
如果您的 getToken()
函数正在从其他 API 获取令牌,那么您应该在其前面添加 await
。
尝试使用下面的代码,
app.use('/', async (req,res,next)=>{
const token=await getToken();
req.headers['authorization']=token;
next();
});
您还需要将 header
替换为 headers
,如上面代码段中所述。
应该可以。
我正在使用 http-proxy-middleware
创建代理,并且 运行 成功。
致电前 app.use('/',proxy_options);
我正在尝试拦截我的请求并修改请求 header 但更新后的值未反映在 headers.
app.use('/',(req,res,next)=>{
const token=getToken();
req.header['authorization']=token;
next();
});
我什至尝试过使用 req.header.authorization=token;
和不使用 next();
。
当我尝试打印时,我的请求 header authorization:''
显示为空白。
谁能告诉我为什么会这样以及我该如何解决这个问题。
必须感谢任何帮助或建议。
如果您的 getToken()
函数正在从其他 API 获取令牌,那么您应该在其前面添加 await
。
尝试使用下面的代码,
app.use('/', async (req,res,next)=>{
const token=await getToken();
req.headers['authorization']=token;
next();
});
您还需要将 header
替换为 headers
,如上面代码段中所述。
应该可以。