Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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