Skip to content

Instantly share code, notes, and snippets.

@marcelitocs
Forked from ryanoglesby08/server.js
Created August 22, 2020 21:12
Show Gist options
  • Save marcelitocs/f5e490c6def0604b4fae3a5b98ed2e80 to your computer and use it in GitHub Desktop.
Save marcelitocs/f5e490c6def0604b4fae3a5b98ed2e80 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