Node.js 在express中缺少区域设置文件时提供404
我用的是DaftMonk的,这很好,除了我有一个小问题 我使用i18next来处理本地化,该库的工作方式是,它尝试从用户给定的本地化设置的指定路径加载一个区域设置文件,即:Node.js 在express中缺少区域设置文件时提供404,node.js,express,yeoman-generator-angular,i18next,Node.js,Express,Yeoman Generator Angular,I18next,我用的是DaftMonk的,这很好,除了我有一个小问题 我使用i18next来处理本地化,该库的工作方式是,它尝试从用户给定的本地化设置的指定路径加载一个区域设置文件,即:/locales/en-US/app.json——如果不存在,它会尝试/locales/en/app.json(在我的例子中,会在这里找到该文件) 问题在于生成器的工作方式是它有一个路径catchall,它将所有404重定向到index.html(angular应用程序的入口点)。这允许单页应用程序的动态路由 当这种情况发生时
/locales/en-US/app.json
——如果不存在,它会尝试/locales/en/app.json
(在我的例子中,会在这里找到该文件)
问题在于生成器的工作方式是它有一个路径catchall,它将所有404重定向到index.html
(angular应用程序的入口点)。这允许单页应用程序的动态路由
当这种情况发生时,i18next
阻塞,因为它需要一个404
或一个json语言环境文件,但得到的是index.html
页面
我需要一种方法,将/locales
路径从这个通配符catch all处理中排除,并在缺少文件时提供404
而不是index.html
页面
以下是express通配符的作用:
//is there anyway to write an exclusion to this `/*` wildcard?
app.route('/*')
.get(middleware.setUserCookie, index.index);
/**
* Send our single page app
*/
exports.index = function (req, res) {
res.render('index', {
name: pkg.name,
version: pkg.version,
env: config.env
});
};
//in dev mode
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'app')));
//in production mode
app.use(express.static(path.join(config.root, 'public')));
如果找不到文件,如何编写将重定向到404的规则,而不是重定向到index.html
if (/\/locales\//.test(req.originalUrl) {
send404function()
}
也许它不是app.use()
,但希望您不必做太多