0

I have an express app set up using http-proxy-middleware, but I'm having a problem routing only a subset of requests through the proxy.

Here is what my config looks like:

app.use(/\/.*-img/i, proxy({changeOrigin: true, logLevel: 'debug', target: 'http://ucassets.blob.core.windows.net'}));

app.get('*', (req, res) => {
    const location = req.url;
    const memoryHistory = createMemoryHistory(req.originalUrl);
    const store = configureStore(memoryHistory);
    const history = syncHistoryWithStore(memoryHistory, store);

    match({history, routes, location},
        (error, redirectLocation, renderProps) => {
            if (error) {
                res.status(500).send(error.message);
            } else if (redirectLocation) {
                res.redirect(302, redirectLocation.pathname + redirectLocation.search);
            } else if (renderProps) {
                /* render HTML and send code 200 */
            } else {
                res.status(404).send('Not Found?');
            }
        });
});

The proxy route sometimes works, and other times ends up printing that "Not found?" 404 result. Of note, there is NOT a route in my react-router config that matches the proxy route above. Since both routes technically match the express route, I guess express is randomly choosing which one to execute?

EDIT: Here's an example route that is being problematic:

/user-img/bc070850-11c9-a79e-2881-9bd6cc04c3ca.jpg

Ben H
  • 3,136
  • 3
  • 25
  • 34
  • Express processes routes in the order they were declared and the first one that matches gets first crack at it. Only if that route calls `next()` do any other matching routes get to see the request. Express does not "randomly" select a route order. – jfriend00 Jun 30 '17 at 21:05
  • What is an example of a URL that you say goes through the proxy sometimes and not others? – jfriend00 Jun 30 '17 at 21:05
  • Thanks for the reply. Here's an example route that is being problematic: /user-img/bc070850-11c9-a79e-2881-9bd6cc04c3ca.jpg – Ben H Jun 30 '17 at 22:30
  • Can you describe exactly what "problematic" means? – jfriend00 Jun 30 '17 at 22:33
  • I mean it behaves in the way I described in the question, namely that sometimes the "*" route matches and returns the 404 result before the proxy handler properly returns the content. – Ben H Jun 30 '17 at 22:34
  • It also seems if I change "app.use" for the proxy handler to "app.get" then it works, but is this acceptable for a middleware? How about for other things like PUT/POST requests? – Ben H Jun 30 '17 at 22:35
  • If your `app.use()` matches the route, then your `app.get('*', ...)` should never get called unless the proxy calls `next()` to tell Express to go to the next router handler. If this is happening, you'll have to delve into the insides of the `proxy()` middleware handler to figure out why. – jfriend00 Jun 30 '17 at 22:42
  • 1
    `app.use()` is used when you want to catch all types of requests, GET, PUT, POST, OPTIONS, etc... `app.get()` is used when you only want to catch GET requests. If `app.get()` works for some reason, then perhaps there is a non-GET request involved that is messing you up somehow. – jfriend00 Jun 30 '17 at 23:20

0 Answers0