From f6b1849bff2677504d9cb01a54740b4b7929472a Mon Sep 17 00:00:00 2001 From: Mars Hall Date: Thu, 9 Feb 2017 19:40:49 -0800 Subject: [PATCH] Support server-side URL routing to React app. --- server/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/index.js b/server/index.js index ba97102..3b4d6d9 100644 --- a/server/index.js +++ b/server/index.js @@ -1,14 +1,23 @@ const express = require('express'); +const path = require('path'); + const app = express(); 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) { res.set('Content-Type', 'application/json'); 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 () { console.log(`Listening on port ${PORT}`); });