Sona Shetty
Sona Shetty

Reputation: 1047

Can't set the header after they are sent to client

I am getting an error "Can't set the header after they are sent to client" when basic authentication and jwt token are not provided in incoming request.

I am getting this error as i am not able to stop node.js from going to verifyJWTToken function when verifyBasicAuth fails.

How do i instruct nodejs not to run verifyJWTToken function when verifyBasicAuth already returned a response?

My route code -

app.post('endpointurl', verifyBasicAuth, verifyJWTToken, postOperation);

Upvotes: 0

Views: 85

Answers (1)

abskmj
abskmj

Reputation: 758

If the implementation of verifyJWTToken middleware is accessible. Add a check if response headers are already sent.

I have assumed the implementation as you have not shared any code.

var verifyJWTToken = function(req,res,next){
    if(!res.headersSent){
      // response headers are not set yet, execute verification  
    }
    else{
      next(); //proceed to next middleware with out sending any response
    }
} 

Upvotes: 1

Related Questions