【问题标题】:Regexp in express get request快速获取请求中的正则表达式
【发布时间】:2015-07-28 13:22:52
【问题描述】:

我使用 expressjs,我想通过 regexp 处理获取请求,如关注

var app = require('express');

app.get(
    '/foo/bar/<here is  picture name e.g. test.jpg>?',
    function(req, res, next) {
        console.log('Hello');
        next();
    }
);

我对正则表达式有疑问。我希望这个正则表达式即使在 md5 和 sha1 哈希中也能传递所有图片名称。

【问题讨论】:

  • 使用[^\/]*(?:jpe?g|png|gif)。如果您需要支持其他扩展,请扩展 nincapturing 组。

标签: regex node.js express get request


【解决方案1】:

您不需要正则表达式,您可以使用URL parameters。

app.get('/foo/bar/:slug',
    function(req, res, next) {
        console.log(req.params.slug);
        next();
    }
);

请求/foo/bar/myImage.jpg 将使用'myImage.jpg' 填充req.params.slug。

【讨论】:

  • 在这种情况下,我可以将任何数字作为 slug 传递,但它不会是图片名称
  • @Mike_Device 然后验证您的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2016-01-15
  • 2011-12-24
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
相关资源
最近更新 更多