Skip to content

Instantly share code, notes, and snippets.

@sylvaindethier
Created June 26, 2017 09:36
Show Gist options
  • Save sylvaindethier/8b37d5fb05acbc443983c75e720e42da to your computer and use it in GitHub Desktop.
Save sylvaindethier/8b37d5fb05acbc443983c75e720e42da to your computer and use it in GitHub Desktop.
NodeJS Express server for SPA
// Express
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const PORT = 9000;
const STATIC = path.resolve(__dirname, 'dist');
const INDEX = path.resolve(STATIC, 'index.html');
const app = express();
app.use(bodyParser.json());
// Static content
app.use(express.static(STATIC));
// All GET request handled by INDEX file
app.get('*', function (req, res) {
res.sendFile(INDEX);
});
// Start server
app.listen(PORT, function () {
console.log('Server up and running on ', `http://localhost:${PORT}/`);
});
@kevin-acom
Copy link

Great example

@rosemdev
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment