How to serve production react bundle.js built by webpack? How to serve production react bundle.js built by webpack? express express

How to serve production react bundle.js built by webpack?


You need to declare a "catch all" route on your express server that captures all page requests and directs them to the client. First, make sure you're including the path module on your server:

var path = require('path');

Then, put this before app.listen:

app.get('*', function(req, res) {  res.sendFile(path.resolve(__dirname, 'public/index.html'));});

This assumes you're inserting bundle.js into index.html via a script tag.