Create-react-app: react proxy does not work

Created on 9 Oct 2018  路  13Comments  路  Source: facebook/create-react-app

Hi i am facing issue with react proxy .. Actually i am accessing files from other port
The proxy works for "/" but not for any other url like "/currentpost/images/"
my proxy code is

  "proxy": {
    "/api/*": {
      "target": "http://localhost:5000"
    },
    "/currentPost/images/*" : {
      "target" : "http://localhost:5000"
    },
    "/images/*": {
      "target": "http://localhost:5000"
    }

}

prob1

and my proxy code in package js file is
ps://user-images.githubusercontent.com/35171108/46656606-dfe41f80-cbc7-11e8-9d1c-9d1713814ca8.PNG)

Most helpful comment

Perhaps this will help you: https://github.com/facebook/create-react-app/issues/5441#issuecomment-429981692

My src/setupProxy.js looks like this now (simplified):

app.use(
  proxy("/rootpath", {
    target: "https://example.org",
    changeOrigin: true,
    onProxyReq(proxyReq) {
      if (proxyReq.getHeader("origin")) {
        proxyReq.setHeader("origin", "https://example.org")
      }
    },
    pathRewrite: { "^/rootpath": "" },
    logLevel: "debug",
  })
)

I recommend setting the logLevel so you'll be aware when a route is or isn't proxied.

All 13 comments

Please upgrade to v2 and follow the migration instructions.

Timer ...................... i up graded but not solved??

Did you follow the migration instructions? The example you posted does not work anymore.

Thanks for reply @Timer ... yes i followed instructions and created new file setupProxy.js but don't know where to include this file .. i.e in package.json or in index.js
image

`const proxy = require('http-proxy-middleware');
module.exports = function(app){

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

}`

Where did you create this file? It doesn't need to be imported anywhere.

in src folder

@Timer here is link of my project repo ... https://github.com/USMAN378/blog-Project- check it

I don't see the proxy route for currentPost configured, that's what's failing in your screenshot.

@Timer but now in the second method the other proxies also not working , that's why i asked you what to do
image

image

hey

@USMAN378
@Timer
I have struggled quite some time with the exact same problem, but then I removed the wild card * and everyhting works fine again.
so try: app.use(proxy('/api/', { target: 'http://localhost:5000' }))

Pierrick

Perhaps this will help you: https://github.com/facebook/create-react-app/issues/5441#issuecomment-429981692

My src/setupProxy.js looks like this now (simplified):

app.use(
  proxy("/rootpath", {
    target: "https://example.org",
    changeOrigin: true,
    onProxyReq(proxyReq) {
      if (proxyReq.getHeader("origin")) {
        proxyReq.setHeader("origin", "https://example.org")
      }
    },
    pathRewrite: { "^/rootpath": "" },
    logLevel: "debug",
  })
)

I recommend setting the logLevel so you'll be aware when a route is or isn't proxied.

Ah yes good point, remove the wild card or make it ** probably.

Well the issue has been solved thanks to all guys especially @Pier2208

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dualcnhq picture dualcnhq  路  3Comments

alleroux picture alleroux  路  3Comments

onelson picture onelson  路  3Comments

ap13p picture ap13p  路  3Comments

Evan-GK picture Evan-GK  路  3Comments