【发布时间】:2020-01-31 06:30:48
【问题描述】:
我是 nodejs 和 typescript 的新手,我想在 req.body 中添加一个新参数,比如 req.body.jwt_token。
我正在使用中间件来更新请求数据模型。 问题是我能够访问(console.log 有效)新密钥 req.body.jwt_token 只是在该功能中工作,除此之外不可访问(甚至不存在)。
我想在某个控制器中使用req.body.jwt_token。
export function httpsProtocol(req: Request, res: Response, next: NextFunction) {
try {
if (req.headers.authorization != undefined) {
let authorization = req.headers.authorization;
let authorizationArr: string[] = authorization.split('Bearer')
if (authorizationArr[1] != undefined) {
let jwtToken = "Bearer " + authorizationArr[1].trim();
req.headers.Authorization = jwtToken;
req.body.jwt_token = authorizationArr[1].trim();
console.log(req.body.jwt_token); //able to console this
}
}
} catch (error) {
return res.status(422).json({
message: "something goes wrong",
error: error
});
}
next();
};
请针对此问题提出解决方案。我如何在 nodejs 和 typescript 中实现这一点。我正在使用 express 作为框架
谢谢
【问题讨论】:
标签: javascript jquery node.js typescript express