I have a React app that is being served by a simple Express server. The React app is being webpack
'd into a directory called dist
in my root directory (where the server.js
file is located as well). The dist folder includes the index.html
entry point, main.min.js
JavaScript bundle, and all other static assets (css, images, etc).
If I visit the root directory of the site, localhost:3000/
for example, the index.html
gets served, it loads the JS bundle and finds all other assets fine. The app, using react-router
, navigates fine through button clicks and a link that brings me to localhost:3000/feature
works fine.
However, if I manually go into the address bar and type in localhost:3000/feature
, the server gives the index.html
as expected, but also serves index.html
in place of main.min.js
. This is because the wildcard route in the express server is mapped to return index.html
. This is how I've seen it done in some articles, but I'm not sure if this case has been considered.
Here's a snippet of the relevant server code:
app.use(express.static(path.join(__dirname)));
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
This is what defines the server routing. Is there a better way to do this that will allow manual address changes?
Here's my file structure if it helps:
|-root
|-server.js
|-webpack.config.js
|-dist/
|-main.min.js
|-index.html
|-a2cea76fae187add3c974dcacf446eab.jpg
|-...etc
Contents of index.html
:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="root"><!-- React app --></div>
<script src="dist/main-631cea2c7e308b1dfb26.min.js"></script>
</body>
</html>