重写Node.js的PHP URL

我需要用Node重写这个URL:

/single.php?articleID=123 

对此:

 /article/123 

这是因为我工作的公司已经打印出了带有旧软件URL的QR码。 现在他们的软件在Node中被重写了,没有QR码了。 我如何使用Node支持这个旧的URL? 我试着build立一条路线:

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

但它只是回应这个:

 Cannot GET /single.php?articleID=12 

有任何想法吗? 谢谢。

快速路由只是path,但你应该能够路由single.php并从req.query获取articleID

 app.get('/single.php', log.logRequest, auth.checkAuth, function (request, reponse) { response.send(request.query.articleID); }); 

如果要为路由请求查询参数,则可以为其创build一个自定义中间件:

 function requireArticleID(req, res, next) { if ('articleID' in req.query) { next(); } else { next('route'); } } app.get('/single.php', requireArticleID, ..., function (request, reponse) { // ... }); 

next('route')在应用程序路由下讨论。