繁体   English   中英

如何在 express.static 中提取请求标头

[英]How to extract request headers in express.static

我正在使用 express.static 中间件,因为我不想在路由中单独列出每个资产。 我所有的路由都是通过 index.html 处理的,因为我使用的是 Vue JS。

由于功能要求,我需要从请求标头中提取一些信息,但我在 express.static 的选项或文档中都没有看到请求。

这是我的代码

const staticFileMiddleware = express.static(__dirname);
app.use(staticFileMiddleware);
app.use(history({
  disableDotRule: true,
  verbose: true
}));

// here I wanted to print out req.headers but its not available anywhere
// console.log(req.headers); <------------------------------

const port = 8080;
app.listen(port, () => {
  console.log(`Example app listening on port ${port}!`);
});

您需要创建一个中间件:

app.use(function (req, res, next) {
  console.log(req.headers)
  next()
})

在节点中,标头仅在请求时可用,因此您需要创建一个有权访问请求的中间件。

app.use((req,res,next) => {
console.log(req.headers)
next()
})

暂无
暂无

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

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