express - 在静态文件中将 content-type 设置为 XHTML
问题描述
我想为我的静态文件设置路由:
// server.js
app.use('/', require('./routes/ui/templates'));
问题是我无法从 html->xhtml 更改内容类型。这是我的路线:
const express = require('express');
const router = express.Router();
// Path configs
const pathRoot = __dirname
const pathPublic = pathRoot + "/../../public/"
router.use('/', express.static(pathPublic));
router.get('/', (req, res) => {
console.log(pathPublic)
res.sendFile('index.html', {root: pathRoot});
})
router.use((req, res, next) => {
res.type('application/xhtml+xml');
next();
})
module.exports = router;
请注意,由于某种原因,如果我不添加router.use(...)
我的索引文件,则根本不会提供服务。据我了解,我编写的中间件应该是最后一个,因为我试图捕获响应并对其进行修改。如果我错了,请纠正我。
解决方案
尝试res.setHeader('content-type', 'application/xhtml+xml');
推荐阅读
- java - 将功能放在 Hashmap 上
- mdx - MSAS m2m 属性未按预期解析
- javascript - facebook按钮不调用onClick() - reactJS
- javascript - Vue2:如何获得平滑的鼠标移动?
- javascript - JavaScript - 原型函数中的访问类实例属性
- android - 当我在清单中更改主题时强制停止
- python - Colaboratory - 记住 Google Drive Auth (Python)
- python - 无法在我的虚拟环境中安装 Tensor Flow
- pentaho - 使用 Java API 运行 Pentaho PDI 作业时找不到类 SimpleContextFactory
- angular - 使用 @Input 属性持续更新子元素时遇到问题