ankitjaininfo
ankitjaininfo

Reputation: 12362

Express: Disable headers for specific routes (Etag, set-cookie, etc)

For my application, we have rest API and the webapp server from the same app. (it is small enought not to have separate deployment)

Is there any way I can exclude all /api/* route paths to disable caching and cookies?

Note: I cannot do app.disable('etag') as it will disable for the entire webapp.

Upvotes: 2

Views: 920

Answers (1)

eol
eol

Reputation: 24555

Afaik this is currently not possible - also there are a few open issues on github like this one for example: https://github.com/expressjs/express/issues/2472

As a workaround you could remove the headers for requests on the /api-route using something like this:

const onHeaders = require('on-headers')

// mount custom middleware for all api-requests
app.use("/api*", (req, res, next) => {
   removeHeaders(res);
   next();
});

function removeHeaders(res) {
  onHeaders(res, () => {
    res.removeHeader('ETag');
    // remove other headers ...
  });
}

Upvotes: 1

Related Questions