2

I need to rewrite this URL with Node:

/single.php?articleID=123

to this:

/article/123

This is because a company I work with already printed out QR codes with said URL for the old software. Now that their software got rewritten in Node, no QR code works anymore. How can I support this old URL with Node? I tried setting up a route for it:

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

But it just responds this:

Cannot GET /single.php?articleID=12

Any ideas? Thanks.

1 Answer 1

2

Express routes are just for paths, but you should be able to route single.php and get articleID from req.query.

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

If you want to require the query parameter for the route, you can create a custom middleware for it:

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

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

next('route') is discussed under Application Routing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.