0

I've been working on a React boilerplate that harnesses Apollo-Client and GraphQL. My app is set up so that I have one node process running an Express server on port 3000 that actually renders the application, and another Express server on port 3001 that uses webpack-dev-middleware to package and serve my JavaScript bundle.

I was getting a 404 when trying to load my bundle using <script src="/static/js/bundle.js />, because it was trying to request the bundle at http://localhost:3000/static/js/bundle.js instead of http://localhost:3001/static/js/bundle.js, where it was actually being served by webpack-dev-middleware.

Is there a way to configure webpack-dev-middleware or my app server so that my app can access the JS bundle from /static/js/bundle.js without having to prepend the http://localhost:3001 in front of it?

Kamaros
  • 4,536
  • 1
  • 24
  • 39

1 Answers1

0

You need to proxy requests from :3000/static/js/bundle.js to :3001/static/js/bundle.js, which you could do with something like this:

const request = require('request');
...
app.get('/static/js/bundle.js', (req, res) => {
  req.pipe(request.get('http://localhost:3001/static/js/bundle.js')).pipe(res);
});

You have make sure that this route is only added during development.

robertklep
  • 198,204
  • 35
  • 394
  • 381