Skip to content

Instantly share code, notes, and snippets.

@medeirosjoaquim
Forked from ryanoglesby08/server.js
Created March 5, 2019 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medeirosjoaquim/a0d79e0eb1a0f4c3ec4226f4b5af9e12 to your computer and use it in GitHub Desktop.
Save medeirosjoaquim/a0d79e0eb1a0f4c3ec4226f4b5af9e12 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