Created
April 9, 2016 01:26
-
-
Save spboyer/1aa7ac47bf0631a30d9eafd7b1af1186 to your computer and use it in GitHub Desktop.
ExpressJS AngularJS Static Server
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
var express = require('express'), | |
path = require('path'), | |
fs = require('fs'); | |
var app = express(); | |
var staticRoot = __dirname + '/'; | |
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 | |
var 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 | |
var ext = path.extname(req.path); | |
if (ext !== ''){ | |
return next(); | |
} | |
fs.createReadStream(staticRoot + 'index.html').pipe(res); | |
}); | |
//app.all('/*', function(req, res, next) { | |
// res.sendFile('index.html', { root: __dirname + '/' }); | |
//}); | |
app.listen(app.get('port'), function() { | |
console.log('app running on port', app.get('port')); | |
}); |
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
{ | |
"name": "ng2-startingline", | |
"version": "1.0.0", | |
"dependencies": { | |
"express": "4.13.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment