create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Proxy config upgraded to 2.x as per instructions and using http-proxy-middleware doesn't work

Open EdBoucher opened this issue 7 years ago • 7 comments

Is this a bug report?

Yes, probably; either that or a documentation issue

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

"Proxy", "Configuring the Proxy Manually"

Environment

Before upgrade to react-scripts 2.1.3:

  System:
    OS: macOS 10.14.2
    CPU: x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 9.10.0 - ~/.nvm/versions/node/v9.10.0/bin/node
    Yarn: 1.13.0 - /usr/local/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v9.10.0/bin/npm
  Browsers:
    Chrome: 72.0.3626.81
    Safari: 12.0.2
  npmPackages:
    react: ^15.4.2 => 15.6.2
    react-dom: ^15.4.2 => 15.6.2
    react-scripts: 1.0.17 => 1.0.17
  npmGlobalPackages:
    create-react-app: Not Found

After upgrade to react-scripts 2.1.3:

  System:
    OS: macOS 10.14.2
    CPU: x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 9.10.0 - ~/.nvm/versions/node/v9.10.0/bin/node
    Yarn: 1.13.0 - /usr/local/bin/yarn
    npm: 5.6.0 - ~/.nvm/versions/node/v9.10.0/bin/npm
  Browsers:
    Chrome: 72.0.3626.81
    Safari: 12.0.2
  npmPackages:
    react: ^15.4.2 => 15.4.2
    react-dom: ^15.4.2 => 15.4.2
    react-scripts: 2.1.3 => 2.1.3
  npmGlobalPackages:
    create-react-app: Not Found

Steps to Reproduce

  1. Take a project with an advanced proxy configuration created with create-react-app 1.x and update the react-scripts package to the latest.

  2. Follow the upgrade instructions. Remove the proxy configuration from package.json. This is the original config (with exact URLs changed to protect the innocent, but retaining structure) :

"proxy": {
    "/api": {
      "target": "http://localhost:9000"
    },
    "/some/thing": {
      "target": "https://example.org:8452/",
      "secure": false,
      "changeOrigin": true
    },
    "/some": {
      "target": "https://example.org:7324",
      "secure": false,
      "changeOrigin": true
    },
    "/.*": {
      "target": "https://example.org:8452/",
      "secure": false,
      "changeOrigin": true
    }
  }
  1. Add http-proxy-middleware to the project. Create a setupProxy.js file in the src folder. Replace each entry as specified, leading to a file looking like this:
const proxy = require('http-proxy-middleware')

module.exports = function(app) {

    app.use('/api', proxy({ target: 'http://localhost:9000/' }))

    app.use('/some/thing', proxy( 
        { 
            target: 'https://example.org:8452/',
            secure: false,
            changeOrigin: true,
            logLevel: 'debug'

        } 
    ))

    app.use('/some', proxy(
        { 
            target: 'https://example.org:7324/',
            secure: false,
            changeOrigin: true,
            logLevel: 'debug'
        } 
    ))

    app.use(proxy('/.*',
        {
            target: 'example.org:8452/',
            secure: false,
            changeOrigin: true,
            logLevel: 'debug'

        }
    ))

}
  1. Attempt to access a resource that was previously working, of the form path/to/some/resource.do?with=some_parameters

Expected Behavior

The resource is obtained as before, and everything works as it did in create-react-app 1.x

Actual Behavior

The resource returned is the index.html file at the root of the project, rather than the endpoint returning json.

Reproducible Demo

Can't really provide one at the minute- things are pretty locked down here and I'd need to spin up a server elsewhere.

Any help greatly appreciated.

EdBoucher avatar Feb 05 '19 11:02 EdBoucher

@EdBoucher I have same problem. Do you have solution for this?

breslauer avatar Mar 21 '19 09:03 breslauer

Sadly no, not yet. Sticking with the old version for now. If you find a way around it do let me know!

EdBoucher avatar Mar 21 '19 10:03 EdBoucher

Have the exact same problem. I'm on react-scripts 2.1.8 if thats relevant.

pacurtin avatar Apr 04 '19 23:04 pacurtin

I found my problem. I had proxy context as "2/feeds" when it needed to be "/2/feeds"

Full working config incase it helps anyone else in future:

const proxy = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    proxy('/2/feeds',
    {
      target: 'https://api-stage.myexamplesite.com',
      logLevel: 'debug',
      secure: false,
      changeOrigin: true,
      onProxyReq(proxyReq) {
        if (proxyReq.getHeader("origin")) {
          proxyReq.setHeader("origin", 'https://api-stage.myexamplesite.com')
        }
      }
    }
    )
  );
};

Frustrating typo mistake because of course it results in no logging output!

@EdBoucher Is it possible the problem is you have a leading "/" on target and a trailing "/" on context, thus incorrectly your full string is concatenated to https://example.org:8452//some/thing (notice the double "//" near the end of the string)?

pacurtin avatar Apr 05 '19 14:04 pacurtin

I have being using setupProxy with no problems. I was using app.use(proxy("/api", { target: "http://localhost:9000/" })); instead of app.use('/api', proxy({ target: 'http://localhost:9000/' }))

EDIT: both ways seem to work well. Please provide a repo case, otherwise I can't help you.

bugzpodder avatar Apr 05 '19 20:04 bugzpodder

Was just an untested suggestion. I'm glad its smart enough to avoid that kind of fault.

pacurtin avatar Apr 05 '19 21:04 pacurtin

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

stale[bot] avatar May 05 '19 21:05 stale[bot]