简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM