Skip to content

Instantly share code, notes, and snippets.

@spboyer
Created April 9, 2016 01:26
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save spboyer/1aa7ac47bf0631a30d9eafd7b1af1186 to your computer and use it in GitHub Desktop.
ExpressJS AngularJS Static Server
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'));
});
{
"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