question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

proxy needs to be last middleware

See original GitHub issue

Perhaps I’m using the library incorrectly, or unaware of its limitations- but I recently found that I’ve had to put http-proxy-middleware as the last middleware in my express config.

Expected behavior

If possible, http-proxy-middleware should call the next() middleware. If not, the behavior should be documented.

Actual behavior

middlewares that follow http-proxy-middleware are not executed

Setup

app.use('*', [
  (req, res, next) => {
    console.log('I do get executed');
    next();
  },

  proxy({ ...proxyOptions }),

  (req, res, next) => {
    console.log('I do not get executed');
    next();
  }
]);
  • http-proxy-middleware: 0.17.4
  • server: express + 4.16.2

proxy middleware configuration

var apiProxy = proxy({
  target:'http://www.example.org',
  changeOrigin:true,
  secure: false
});

server mounting

const https = require('https');
const express = require('express');
const app = express();
const certs = require('./getCertOptions')();
const middlewares = require('./getExpressMiddlewares')();

app.use('*', middlewares);

https.createServer({ certs }, app).listen(5443);

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:15
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
PaulMalycommented, Oct 4, 2021

@aral @hpl002 Hi guys, I’m pretty sure it should work:

    app.use((req, res, next) => {
        createProxyMiddleware(path,  {
            selfHandleResponse: true,
            onProxyRes(proxyRes, req, res) {
                if (proxyRes.statusCode === 404) {
                    return next();
                } else {
                    let body = new Buffer('');
                    proxyRes.on('data', (data) => body = Buffer.concat([body, data]));
                    proxyRes.on('end', () => res.end(body));
                }
            }
        })(req, res, next);
    });

What do you think about this solution? The weak point is that HPM will be created on each request.

2reactions
hpl002commented, Jan 6, 2021

Been a while!

Any updates on this. I too was a bit surprised to see that my subsequent middleware suddenly failed. Would be nice to at least have the argument available in the case of option.selfHandleResponse.

Closing by pending issue provided that there might be more progress here…

One dirty workaround that might work for some is to create a separate express instance which only handles proxying. This instance can then be called in the middleware of the outer instance.

My usecase simply being response validation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

http-proxy-middleware - npm
The one-liner node.js proxy middleware for connect, express and browser-sync. Latest version: 2.0.6, last published: 8 months ago.
Read more >
http-proxy-middleware not proxying requests if placed after ...
If i move the proxy middleware after the cookie parser it does not proxy requests silently fails without any errors.I tried creating a...
Read more >
Middleware
Custom Proxy Middleware ... Middlware inside the MapReverseProxy pipeline have access to all of the proxy data and state associated with a request...
Read more >
sap-ux/backend-proxy-middleware - NPM Package Overview
If the backend destination is configured to use principal propagation, then in some cases the requests might fail. If this occurs then you...
Read more >
http-proxy-middleware - npm Package Health Analysis - Snyk
We found that http-proxy-middleware demonstrates a positive version release cadence with at least one new version released in the past 12 months. As...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found