Support server-side URL routing to React app.
This commit is contained in:
@@ -1,14 +1,23 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 5000;
|
const PORT = process.env.PORT || 5000;
|
||||||
|
|
||||||
app.use(express.static(__dirname + '/../react-ui/build'));
|
// Priority serve any static files.
|
||||||
|
app.use(express.static(path.resolve(__dirname, '../react-ui/build')));
|
||||||
|
|
||||||
|
// Answer API requests.
|
||||||
app.get('/api', function (req, res) {
|
app.get('/api', function (req, res) {
|
||||||
res.set('Content-Type', 'application/json');
|
res.set('Content-Type', 'application/json');
|
||||||
res.send('{"message":"Hello from the custom server!"}');
|
res.send('{"message":"Hello from the custom server!"}');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// All remaining requests return the React app, so it can handle routing.
|
||||||
|
app.get('*', function(request, response) {
|
||||||
|
response.sendFile(path.resolve(__dirname, '../react-ui/build', 'index.html'));
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(PORT, function () {
|
app.listen(PORT, function () {
|
||||||
console.log(`Listening on port ${PORT}`);
|
console.log(`Listening on port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user