重写Node.js的PHP URL

重写Node.js的PHP URL,node.js,url-rewriting,express,Node.js,Url Rewriting,Express,我需要用节点重写此URL: /single.php?articleID=123 为此: /article/123 这是因为与我合作的一家公司已经为旧软件打印出带有上述URL的二维码。现在,他们的软件在Node中被重写,QR码就不再有效了。如何使用节点支持此旧URL?我试着为它设置一条路线: app.get('/single.php?articleID=:id', log.logRequest, auth.checkAuth, function (request, reponse) { r

我需要用节点重写此URL:

/single.php?articleID=123
为此:

/article/123
这是因为与我合作的一家公司已经为旧软件打印出带有上述URL的二维码。现在,他们的软件在Node中被重写,QR码就不再有效了。如何使用节点支持此旧URL?我试着为它设置一条路线:

app.get('/single.php?articleID=:id', log.logRequest, auth.checkAuth, function (request, reponse) {
  response.send(request.params.id);
});
但它只是回应:

Cannot GET /single.php?articleID=12

有什么想法吗?谢谢。

快速路线仅适用于路径,但您应该能够路由
single.php
并从中获取
articleID

如果需要路由的查询参数,可以为其创建自定义中间件:

function requireArticleID(req, res, next) {
    if ('articleID' in req.query) {
        next();
    } else {
        next('route');
    }
}

app.get('/single.php', requireArticleID, ..., function (request, reponse) {
    // ...
});
next('route')
在下面讨论

function requireArticleID(req, res, next) {
    if ('articleID' in req.query) {
        next();
    } else {
        next('route');
    }
}

app.get('/single.php', requireArticleID, ..., function (request, reponse) {
    // ...
});