我正在构建一个单页应用,使用node(v7.7.1)作为服务器并使用express进行路由和API构建。
我的前端代码打包成一个名为/client
的文件夹(其中包含所有JS、css、img和html)。我的API的根url为/api
。
任何不是文件或API的其他查询都应发送到index.html
。因此,我尝试了以下方法:
app.use(express.static(path.join(__dirname, '../client')));
app.get('*', function (req, res, next) {
if (req.originalUrl.indexOf('/api') !== -1) {
return next()
}
if (/\/(\w|-)+\.\w{2,5}$/gmi.test(req.originalUrl)) {
return res.sendFile(rss);
}
res.sendFile(path.join(__dirname, '../client/index.html'));
});
上述代码会在浏览器控制台中给我报错,错误类型为mime
类型错误,对于css和js文件(可能还包括图片文件),因为没有加载成功,所以无法确定。
浏览器控制台:
Refused to apply style from '/app-2432393bf1588983d4f6.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
任何关于如何更好地完成此操作或如何修复问题的提示都将不胜感激。
style.css
文件的源代码时,它是我的html页面。 - denislexic