Skip to content

Commit

Permalink
To handle the routes on the client (#786)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dplopezsioux committed Apr 27, 2024
1 parent 23073b7 commit 654a86d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/webpack-hot-middleware/server.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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!'));

0 comments on commit 654a86d

Please sign in to comment.