Search code examples
reactjsexpresswebserverstatic-files

Serve static bundle.js from remote file via Express as your static file server


I am trying to serve a React front-end (bundle.js) with an Express back-end.

The repositories exist independently of one another. The static React bundle file is hosted remotely on a separate file server (nginx).

I want Express to be both the file server that serves the static file, as well as the application server responsible for the api endpoints.

I know about express.static() method, however, that would only work if the bundle file is in the same directory as the back-end. But in my case the repos are separate. How can I achieve this?

UPDATE:

So the only solution to this is to have the build folder included in the deployed Express-backend. I simply had Webpack spit out the bundle.js to the back-end repo, and am not using express.static() method to successfully serve it.


Solution

  • you can do this

    app.use('/', express.static(path.join(__dirname, '../frontend/dist')));

    the frontend folder is not in the same folder as server.js but in the parent folder I'm doing this in my MEAN app and it works