Skip to content

Instantly share code, notes, and snippets.

@feedsbrain
Last active September 5, 2019 10:08
Show Gist options
  • Save feedsbrain/03c74178b6f0b5369b6a046373e4a7a0 to your computer and use it in GitHub Desktop.
Save feedsbrain/03c74178b6f0b5369b6a046373e4a7a0 to your computer and use it in GitHub Desktop.
Angular 2
const express = require('express')
const path = require('path')
const fs = require('fs')
const app = express()
const staticRoot = path.resolve() // this is you app root
app.set('port', process.env.PORT || 3000)
app.use(express.static(staticRoot))
app.use(function (req, res, next) {
// if the request is not html then move along
const accept = req.accepts('html', 'json', 'xml')
if (accept !== 'html') {
return next()
}
// if the request has a '.' assume that it's for a file, move along
const ext = path.extname(req.path)
if (ext !== '') {
return next()
}
fs.createReadStream(staticRoot + 'index.html').pipe(res)
})
app.listen(app.get('port'), function () {
console.log('app running on port', app.get('port'))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment