Set header in node.js express framework middleware

I have middleware running some auth. In this auth method, I need to set the response header.

server.get('/api/users, auth(), getUsers);

My machine:

module.exports = (isProduction) => {
  return function(req, res, next){
     ...

     next();

  }
}

How to add a title to this auth function?

+4
source share
2 answers

I assume you are using express.js. There is a function in the expression set(documentation here . You can use it like this:

res.set('<header name>', '<header value>')

before calling next()

+3
source

This is not a key question node.js, but a question on express.js.

In this case, your link: http://expressjs.com/en/4x/api.html#res.set

The signature of the set () function: res.set(field [, value])

res node native http.response api.

.set() express.js, , next(), .

, :

res.set('Content-Type', 'text/plain');

res.set({
  'Content-Type': 'text/plain',
  'Content-Length': '123',
  'ETag': '12345'
});
0

Source: https://habr.com/ru/post/1667738/


All Articles