【问题标题】:Modifying req.path in expressjs middleware在 expressjs 中间件中修改 req.path
【发布时间】:2016-06-21 01:50:42
【问题描述】:

我正在尝试制作一个中间件来从路径中删除语言环境字符串(例如/de/about -> /about),并且我正在使用 express。我尝试了以下中间件:

app.use(function (req, res, next) {
    var localeMatch = /^\/([a-z]{2}(?:\-[A-Z]{2})?)(\/.+)$/.exec(req.path);

    if (localeMatch) {
        req.locale = localeMatch[1];
        req.path = localeMatch[2];
    } else {
        req.locale = 'en-GB';
    }

    next();
});

它不起作用,因为req.path 是只读的。我该怎么做?

【问题讨论】:

    标签: node.js express routing


    【解决方案1】:

    看看 Express'mount paths。您可以在每个“子应用程序”上拥有一个处理功能,但让您的所有语言环境都走同一条路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2023-01-21
      • 2017-11-14
      相关资源
      最近更新 更多