首先,将Passport中间件用于用户授权处理是一个好习惯。它需要解析您的请求的所有繁琐工作,还提供许多授权选项。现在为您的Node.js代码。您需要使用jwt方法验证并解析传递的令牌,然后通过从令牌中提取的ID查找用户:
exports.me = function(req,res){ if (req.headers && req.headers.authorization) { var authorization = req.headers.authorization.split(' ')[1], depred; try { depred = jwt.verify(authorization, secret.secretToken); } catch (e) { return res.status(401).send('unauthorized'); } var userId = depred.id; // Fetch the user by id User.findOne({_id: userId}).then(function(user){ // Do something with the user return res.send(200); }); } return res.send(500);}


