Skip to content

Instantly share code, notes, and snippets.

@giacomorebonato
Created September 29, 2016 11:55
Show Gist options
  • Save giacomorebonato/49919551d02fc82f6ef0e2ca0095aa6f to your computer and use it in GitHub Desktop.
Save giacomorebonato/49919551d02fc82f6ef0e2ca0095aa6f to your computer and use it in GitHub Desktop.
simple ExpressJS spa
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const app = express()
// assets. Static JS, CSS, fonts
app.use('/public', express.static(path.join(__dirname, '../public')))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// SPA default configuration
// it is important to declare this after the assets rule
app.get('*', (req, res, next) => {
res.sendFile('path to your html file')
})
// start server
const server = app.listen(process.env.PORT || 3000, () => {
console.log(`server running on port ${server.address().port}`)
})
@giacomorebonato
Copy link
Author

Then the start script in package.json should be something like node ./server/server.js

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