Skip to content

Instantly share code, notes, and snippets.

@kevin-bruton
Forked from ryanoglesby08/server.js
Created June 8, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevin-bruton/1aa1946e8de3b2438fc11fbe432a8ead to your computer and use it in GitHub Desktop.
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.
/*
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