From 654a86dc8d989a084342273d63453c3a042e9309 Mon Sep 17 00:00:00 2001 From: Daniel <38842757+dplopezsioux@users.noreply.github.com> Date: Sat, 27 Apr 2024 18:11:05 -0500 Subject: [PATCH] To handle the routes on the client (#786) To handle the routes on the client, if the user forces the URL on the client side, Express will send the React app to the front-end, and then the application can handle the route in the front-end. --- examples/webpack-hot-middleware/server.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/examples/webpack-hot-middleware/server.js b/examples/webpack-hot-middleware/server.js index b8821626..9524a7c3 100644 --- a/examples/webpack-hot-middleware/server.js +++ b/examples/webpack-hot-middleware/server.js @@ -1,6 +1,7 @@ const express = require('express'); const webpack = require('webpack'); const config = require('./webpack.config.js'); +const path = require('path'); const app = express(); const compiler = webpack(config); @@ -22,4 +23,18 @@ app.use( }) ); +app.get("*", (req, res, next) => { + const filename = path.join(compiler.outputPath, "index.html"); + compiler.outputFileSystem.readFile(filename, (err, result) => { + if (err) { + return next(err); + } + res.set("content-type", "text/html"); + res.send(result); + res.end(); + }); +}); + + + app.listen(8080, () => console.log('App is listening on port 8080!'));