0

So I would like to find a way how to implement this part of package.json to my server.js which is responsible for my React App server side rendering.

"proxy": {
    "/api/*": {
      "target": "http://localhost:3501"
    },
    "/media/*": {
      "target": "http://localhost:3501"
    }
  },

I have looked into some of the libraries like express-http-proxy and http-proxy-middleware but I cannot find a working solution.

Last I tried:

import proxy from 'express-http-proxy';
...

app.use('/api', proxy('http://localhost:3501/api/*'));

app.use('/media', proxy('http://localhost:3501/media/*'));

It logs 404 and the path is basically right, just without "/api/" should be "/api/posts/" but logs only "/posts/".

1
  • 1
    Why do you need the proxy? Maybe there is a workaround so that you don't have to use one. Is it a MERN stack? Commented Sep 12, 2018 at 16:23

1 Answer 1

3

Use http-proxy-middleware

It is just as simple as that. Replace your current code with this:

import proxy from 'http-proxy-middleware';

...

app.use('/media/*', proxy({target: 'http://localhost:3500', changeOrigin: true}));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.