如何在nodejs的快速路由中设置报头



我正在研究express js,在传入的POST请求中,用户名和密码存在于请求的正文中,我想实现路由,以便可以将授权头添加到传入的req对象

我的路由如下:

router.route('/token')
  .post(function(req,res,next){
    if(req.body.client_id){
      //set headers for authentication, e.g "Authorization":"Basic dskvnksnsnjsnvsnlvnsd"
      next();      
    }
  },authController.isClientAuthenticated,oauth2Controller.token);

您可以使用req.headers属性添加标题:req.headers.authorization = 'Basic ...'

注意,有一个res.headersSent属性,它可以用来确定是否已经发送到客户端,否则你会遇到一个错误。

相关内容

最新更新