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 rewrite function no longer called in 1.15.0

See original GitHub issue

Hey there,

in 1.14.1 the following rewrite function worked

  proxy: {
      '/api/**': {
        target: 'http://localhost:9001',
        rewrite: function(req) {
          req.url = req.url.replace(/^\/api(.+)$/, '$1');
          return req.url;
        },
        ignorePath: false,
        changeOrigin: true,
        secure: false
      }
    }

As of 1.15.0 The rewrite function doesn’t get called anymore.

Was there a breaking change with how the rewrite function works? I understand there was a switch in how proxy works but I figured I would have to update to 2.x.x for those changes to manifest.

Thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
chimuraicommented, Aug 24, 2016

@SpaceK33z @jdkrebs

This line in http-proxy-middleware kind of ignores/reverts the rewritten path in your fix. (source)

// http-proxy-middleware
req.url = (req.originalUrl || req.url);

Not sure what the best approach would be to make it backwards compatible.

As alternative, you could use the .pathRewrite option offered by HPM. The syntax is slightly different…

    proxy: {
      '/api/**': {
        target: 'http://localhost:9001',
        pathRewrite: function (path, req) {
          return path.replace('/api/foo', '/api/bar');
        }
      }
    }

For more info on pathRewrite: http-proxy-middleware/recipes/pathRewrite.md


@SpaceK33z

I noticed a sudden surge in downloads since v1.15.0 got released and with it also a number of proxy related issues. Feel free to cc me if you need any assistance.

0reactions
SpaceK33zcommented, Aug 26, 2016

Closing this. This breaking change was not intended to be released in a minor release, but to completely revert this we’d need to revert the whole PR that introduced http-proxy-middleware. This also brought us websocket proxy support, and a lot of other nice options. Also, not that many people seem to use rewrite. Documentation for the dev-server is updated to reflect this.

Thanks for your help @jdkrebs and @chimurai

TL;DR - use pathRewrite, as documented in http-proxy-middleware.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove matched `/api` path in Webpack proxy - Stack Overflow
I've added the rewrite to the docs. As bypass already passes the response as an argument to the registered handler, I'm unsure how...
Read more >
http-proxy - npm
It is then possible to proxy requests by calling these functions. http.createServer(function(req, res) {. proxy.web(req, res, ...
Read more >
Go 1.15 Release Notes - The Go Programming Language
A TestMain function is no longer required to call os. Exit . If a TestMain function returns, the test binary will call os....
Read more >
nghttpx - HTTP/2 proxy - HOW-TO — nghttp2 1.52.0-DEV ...
In this mode, it works as reverse proxy (gateway) for HTTP/3, HTTP/2 and HTTP/1 clients to backend servers. This is also known as...
Read more >
CHANGES — oslo.cache 1.15.0 documentation
Make CacheIsolatingProxy public; Add shields.io version/downloads links/badges into README.rst; add auto-generated docs for config options; Change ...
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