首页 > 解决方案 > 在静态文件中将 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(...) 我的索引文件,则根本不会提供服务。据我了解,我编写的中间件应该是最后一个,因为我试图捕获响应并对其进行修改。如果我错了,请纠正我。

标签: express

解决方案


尝试res.setHeader('content-type', 'application/xhtml+xml');


推荐阅读