0

我正在使用 DaftMonk 的generator-angular-fullstack,它非常棒,除了我遇到的一个小问题......

我正在使用 i18next 来处理本地化,该库的工作方式是尝试从您指定的路径为用户给定的本地化设置加载语言环境文件,即:/locales/en-US/app.json- 如果不存在,它会尝试/locales/en/app.json(并在我的案例在这里找到了文件)。

问题是生成器的工作方式是它有一个路由包罗万象,它将所有 404 重定向到index.html(角度应用程序的入口点)。这允许单页应用程序的动态路由。

发生这种情况时,会i18next因为它期待一个404或一个 json 语言环境文件而窒息,但却得到了index.html页面。

我需要一种方法来排除/locales该通配符处理的路径,如果文件丢失,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')));

/locales/*如果找不到文件,我如何编写将重定向到服务 404 的规则,而不是重定向到index.html

4

1 回答 1

0

关于什么

if (/\/locales\//.test(req.originalUrl) {
    send404function()
}

也许不是app.use(),但希望你不必做太多。

于 2015-01-24T16:06:54.200 回答