我正在使用JSON服务器来模拟React应用程序的后端。我需要为 /index.html 路径应用自定义中间件,该路径用于应用程序检索CSRF令牌。我尝试了以下方法:
module.exports = (req, res, next) => {
console.log("middleware-csrf", req, res, next)
if (req.method === 'GET' && req.path === '/index.html') {
res.header('X-CSRF-TOKEN', 'csrf-mock')
res.type('html')
res.status(200).end()
} else {
next()
}
}
但是该请求仍返回JSON-Server自己的首页,而没有访问中间件。是否可以使用JSON-Server模拟 index.html 路径?