Skip to content

Instantly share code, notes, and snippets.

@jeffscottward
Last active May 2, 2017 06:11
Show Gist options
  • Save jeffscottward/10419517 to your computer and use it in GitHub Desktop.
Save jeffscottward/10419517 to your computer and use it in GitHub Desktop.
Express 4 version of app.js of "How I Setup Angular + Node Projects" by J Cole Morrison
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
/**
* Development Settings
*/
if ('development' == app.get('env')) {
// This will change in production since we'll be using the dist folder
app.use(express.static(path.join(__dirname, '../client/.tmp')));
// This covers serving up the index page
app.use(express.static(path.join(__dirname, '../client/app')));
// This is the new way to handle errors in Express 4. not errorHandler().
// For more about error-first best practices see http://fredkschott.com/post/2014/03/understanding-error-first-callbacks-in-node-js/
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
}
/**
* Production Settings
*/
if('production' == app.get('env')) {
app.use(express.static(path.join(__dirname, '/dist')));
}
/* Add this to fire the server */
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
module.exports = app;
@rsrivastava76
Copy link

Thanks for updated version, btw this also need to updated as Favicon() is now required path as must have input.

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