javascript - 如何在 express.static 中提取请求标头
问题描述
我正在使用 express.static 中间件,因为我不想在路由中单独列出每个资产。当我使用 Vue JS 时,我所有的路由都是通过 index.html 处理的。
由于功能要求,我需要从请求标头中提取一些信息,但我在 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()
})
推荐阅读
- javascript - 角度 CRUD 模态
- java - 如何使用 ByteArray 进行对象序列化和反序列化
- c - 从文件中读取数据并保存在 C 中的列表中
- pypdf2 - 使用 utf8 编码在 Pypdf 中插入徽标
- c - 我应该如何决定释放链表节点中数据的责任?
- c# - ASP.Net (not Core) Web API - 将 Camel 格式的 JSON 答案更改为 Pascal 格式
- asp.net-mvc - 如何在Core中接收来自银行的验证数据
- assembly - 程序集 8086 - 查找指令结果
- python - Excel单元格到Python中的列表
- json - 如何从服务器中的 JSON 文件中获取音轨?