【问题标题】:How to use the local Parameters in Routes如何在路由中使用本地参数
【发布时间】:2017-08-09 02:07:29
【问题描述】:

每个人, 我在获取本地参数然后在路由中传递它时遇到问题。 我在 routes/middleware.js 中有这段代码

exports.detectLang = function(req, res, next) {
var match = req.url.match(/^\/(de|en)([\/\?].*)?$/i);

if (match) {
    req.setLocale(match[1]);
    // Make locale available in template
    // (necessary until i18n 0.6.x)
    res.locals.locale = req.getLocale();
    // reset the URL for routing


    req.url = match[2] || '/';
    console.log(response);
} else {
    // Here you can redirect to default locale if you want
    console.log("does not match")
}

next();
     };

这会检测 url 中的 de 或 en 部分,并使用 de 或 en 设置局部变量“Locale”

现在在 routes/view/index.js 中

我想获取本地参数“Locale”并将其放入路由中 像这样的

    // Setup Route Bindings
exports = module.exports = function (app) {
    // Views

    app.get('/'+locale, routes.views.index);

    app.all('/'+locale+'employees', routes.views.employees);

};

但我无法从中间件获取该语言环境参数到 index.js

【问题讨论】:

  • 为什么不使用 cookie?

标签: node.js keystonejs


【解决方案1】:

您可以在路线中使用参数“locale”:

app.get('/:locale', routes.views.index);

...然后使用中间件中的参数:

var match = req.params.locale.match(/^\/(de|en)([\/\?].*)?$/i);

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 2015-12-01
    • 2020-12-05
    • 2011-10-04
    • 2020-10-30
    • 2017-10-07
    • 1970-01-01
    • 2016-09-09
    • 2019-01-30
    相关资源
    最近更新 更多