简体   繁体   English

Express:禁用特定路由的标头(Etag、set-cookie 等)

[英]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.对于我的应用程序,我们有来自同一个应用程序的 rest API 和 webapp 服务器。 (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?有什么办法可以排除所有/api/*路由路径以禁用缓存和 cookie?

Note: I cannot do app.disable('etag') as it will disable for the entire webapp.注意:我不能做app.disable('etag')因为它会禁用整个 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 Afaik 这目前是不可能的 - 在 github 上也有一些未解决的问题,例如: 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:作为一种解决方法,您可以使用以下内容删除/api -route 上请求的标头:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点 js express-session Set-Cookie on Postman/不在 Flutter http 标头中 - node js express-session Set-Cookie on Postman/ Not in Flutter http headers res.setHeader(“ Set-Cookie”,…)未在Node / Express中设置cookie - res.setHeader(“Set-Cookie”, …) is not setting the cookie in Node / Express 当 SameSite 为 none 时,Express 不会设置 cookie - Express doesn't set-cookie when SameSite is none React/Express set-cookie 不能跨不同的子域工作 - React/Express set-cookie not working across different subdomains Cookie设置在位置“ / set-cookie”,但无法使用express.js在“ / user”处检索。 - A cookie is set at location `/set-cookie` but cannot be retrieved at `/user` using express.js 从请求标头中获取特定的cookie(快速) - Get specific cookie from request headers (express) 如何将 JWT 令牌从标头 Set-Cookie 传递到 Strapi 中的授权标头 - How to pass JWT token from Headers Set-Cookie to Authorization Headers in Strapi 特定路线的快速设置会话 - Express Set Session for Specific Routes 如何在快递路线Node.js中设置标头 - How to set headers in express routes nodejs 为什么在使用 setRequestHeader 制作 xmlhttprequest 时无法设置 cookie 和 set-cookie 标头? - Why cookies and set-cookie headers can't be set while making xmlhttprequest using setRequestHeader?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM