Created
June 26, 2017 09:36
-
-
Save sylvaindethier/8b37d5fb05acbc443983c75e720e42da to your computer and use it in GitHub Desktop.
NodeJS Express server for SPA
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
// 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}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example