DEV Community

Discussion on: Using Create-React-App with Express

Collapse
 
jaspersurmont profile image
Jasper Surmont

Hey,
When I try to serve my production build app from localhost (here 8014) I just get a blank page. In the console i get the error message: GET localhost:8014/static/js/2.2612b9d... net::ERR_ABORTED 404 (Not Found)
Any idea?

Collapse
 
bordenjardine profile image
BordenJardine • Edited

the express server doesn't know how to host the static files generated by created webpack.
put app.use(express.static('build')) to tell express where to look for them.
@Lou maybe this should be included in the article.

Collapse
 
loujaybee profile image
Lou (🚀 Open Up The Cloud ☁️)

Thanks @bordenjardine . The post does currently refer to the build folder:

app.use(express.static(path.join(__dirname, 'build')));

But depending on how you're referencing the build folder it may depend on where you're the server start commands from. Try as @jaspersurmont to ensure that you're correctly pointing to the build folder, and that it has at the least an index.html inside.

Collapse
 
casal0x profile image
Sebastian Eduardo Casal

did you find a solution for this? i having the same problem.

Collapse
 
jaspersurmont profile image
Jasper Surmont

I did fix it, however I'm not so sure what exactly I did.
I think that one of the issues was that I didn't refer to the build folder correctly. Try messing around with that a bit

Collapse
 
thomasgauvin profile image
Thomas Gauvin

leaving this here for anyone that might be encountering the same issue! Here's how I fixed it, I just needed to add a route that matches any (*) that don't match exactly '/', and serve the react app:

app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

// Handles any requests that don't match the ones above
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

Cheers!

Collapse
 
i3ats profile image
i3ats

(*) did the trick for me too :)