nikhil.g777
nikhil.g777

Reputation: 904

How to set headers in express routes nodejs

I am working on express js and in the incoming POST request, the username and password are present in the body of the request, I want to implement routing such that an authorisation header can be added to the incoming req object

My routing is as follows :

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);

Upvotes: 1

Views: 1186

Answers (1)

DevDig
DevDig

Reputation: 1018

You can add headers by using the req.headers property: req.headers.authorization = 'Basic ...'

Note that there is a res.headersSent property, which can be used to determine if headers have already been sent to the client, otherwise you will encounter an error.

Upvotes: 1

Related Questions