TypeError:无法读取未定义的属性'header'

问题描述 投票:0回答:1

我正在使用JWT授权开发一个简单的API,并尝试制作一个验证中间件。显然,我在导出中间件功能时收到错误:

TypeError: Cannot read property 'headers' of undefined

middlewares / auth.js:

const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
  const token = req.headers['authorization'];
  console.log('Authorization token => ', token);
  if (!token) {
    res.status(403).json({ message: 'Forbidden' });
  } else {
    const verified = jwt.verify(token, process.env.SECRET_KEY);
    req.verified = verified;
    next();
  }
};

routes / user.js:

const authMiddleware = require('../middlewares/auth');

router.use(authMiddleware());
node.js express jwt router middleware
1个回答
0
投票

代替req.headers['authorization']使用req.query['authorization']


推荐问答