OneCompiler

[NodeJS] Express not able to get the static files of React

I'm working on a React Node project where I'm using the below code to get all the static files. I'm using webpack+babel to create the bundle.js.

Folder structure:

Project
-Client
 /build
   bundle.js
   index.html
-routes
  index.js // routes are defined here and I'm using the below code to load static files

code to load static files:

router.use(express.static(__dirname + '../Client/build'));

For some strange reason, I'm able to hit index.html but unable to load bundle.js.

1 Answer

4 years ago by

Try checking the path of bundle.js from your app. I've faced a similar issue, where I have to specify the path like below which solved my issue, this depends on how your configurations are set.

router.use('/static',express.static(__dirname + '../Client/build'));
4 years ago by Divya