-
-
Save kevin-bruton/1aa1946e8de3b2438fc11fbe432a8ead to your computer and use it in GitHub Desktop.
A node.js SPA server that serves static files and an index.html file for all other routes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Incredibly simple Node.js and Express application server for serving static assets. | |
Given as an example from the React Router documentation (along with examples | |
using nginx and Apache): | |
- https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#browserhistory | |
*/ | |
const express = require('express'); | |
const path = require('path'); | |
const port = process.env.PORT || 8080; | |
const app = express(); | |
// serve static assets normally | |
app.use(express.static(__dirname + '/dist')); | |
// handle every other route with index.html, which will contain | |
// a script tag to your application's JavaScript file(s). | |
app.get('*', function (request, response) { | |
response.sendFile(path.resolve(__dirname, 'index.html')); | |
}); | |
app.listen(port); | |
console.log("server started on port " + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment