Skip to content

Commit 654a86d

Browse files
authored
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.
1 parent 23073b7 commit 654a86d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

examples/webpack-hot-middleware/server.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const webpack = require('webpack');
33
const config = require('./webpack.config.js');
4+
const path = require('path');
45

56
const app = express();
67
const compiler = webpack(config);
@@ -22,4 +23,18 @@ app.use(
2223
})
2324
);
2425

26+
app.get("*", (req, res, next) => {
27+
const filename = path.join(compiler.outputPath, "index.html");
28+
compiler.outputFileSystem.readFile(filename, (err, result) => {
29+
if (err) {
30+
return next(err);
31+
}
32+
res.set("content-type", "text/html");
33+
res.send(result);
34+
res.end();
35+
});
36+
});
37+
38+
39+
2540
app.listen(8080, () => console.log('App is listening on port 8080!'));

0 commit comments

Comments
 (0)