0

I have a middleware function that takes a query string value and turns it into

I am using Express 4 and Node.js. I have a route defined for /:module/:page.aspx and am using req.params and req.query values across a number of middleware functions.

app.get('/:module/:page.aspx', catchEncodedQuery, doSomeStuff, doOtherStuff);

I have one middleware function that appears first, which catches certain requests which have a query string value that can be transformed into a new req.url that I set for use further on in the middleware chain. Basically, I want to proceed as though the un-encoded form of this query string value was the request URL the whole time.

function catchEncodedQuery(req, res, next) {
    if (
        // req meets some specific criteria
    ) {
        req.url = myUnencodeFunction(req.query.url_str);
        next();
    } else {
        next();
    }
};

But I would also like req.params and req.query to reflect this new URL. I could just parse the new URL myself and reassign these values to req.params and req.query, but I'm wondering if there's a better way to do this that leverages Express's existing URL-parsing functionality.

NReilingh
  • 1,730
  • 17
  • 32
  • Possible duplicate of [Forward request to alternate request handler instead of redirect](https://stackoverflow.com/questions/18136323/forward-request-to-alternate-request-handler-instead-of-redirect) – NReilingh Apr 12 '19 at 01:52
  • Voting to self-close as duplicate of "internal redirect" question. I had assumed that Express lines up all available routes at the outset, but it looks like changing `req.url` and then calling `next('route');` will match routes to the changed `req.url`! – NReilingh Apr 12 '19 at 01:53

0 Answers0