Created
July 22, 2014 05:23
-
-
Save outrightmental/ca579241a92097bf18dd to your computer and use it in GitHub Desktop.
Node.js & Express configuration from [email protected]
This file contains hidden or 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
/** | |
* Main application file | |
*/ | |
'use strict'; | |
// Set default node environment to development | |
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; | |
var express = require('express'); | |
var mongoose = require('mongoose'); | |
var config = require('./config/environment'); | |
// Connect to database | |
mongoose.connect(config.mongo.uri, config.mongo.options); | |
// Setup server | |
var app = express(); | |
var server = require('http').createServer(app); | |
var socketio = require('socket.io').listen(server); | |
require('./config/socketio')(socketio); | |
require('./config/express')(app); | |
require('./routes')(app); | |
// Start server | |
server.listen(config.port, config.ip, function () { | |
console.log('Express server listening on %d, in %s mode', config.port, app.get('env')); | |
}); | |
// Expose app | |
var exports = module.exports = app; |
This file contains hidden or 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
/** | |
* Express configuration | |
*/ | |
'use strict'; | |
var express = require('express'); | |
var favicon = require('static-favicon'); | |
var morgan = require('morgan'); | |
var compression = require('compression'); | |
var bodyParser = require('body-parser'); | |
var methodOverride = require('method-override'); | |
var cookieParser = require('cookie-parser'); | |
var errorHandler = require('errorhandler'); | |
var path = require('path'); | |
var config = require('./environment'); | |
var passport = require('passport'); | |
//Sessions | |
var session = require('express-session'); | |
var mongoStore = require('connect-mongo')(session); | |
module.exports = function (app) { | |
var env = app.get('env'); | |
app.set('views', config.root + '/server/views'); | |
app.set('view engine', 'jade'); | |
app.use(compression()); | |
app.use(bodyParser()); | |
app.use(methodOverride()); | |
app.use(cookieParser()); | |
app.use(passport.initialize()); | |
// Persist sessions with mongoStore | |
app.use(session({ | |
secret: config.secrets.session, | |
store: new mongoStore({ | |
url: config.mongo.uri, | |
collection: 'sessions' | |
}, function () { | |
console.log('db connection open' ); | |
}) | |
})); | |
if ('production' === env) { | |
app.use(favicon(path.join(config.root, 'public', 'favicon.ico'))); | |
app.use(express.static(path.join(config.root, 'public'))); | |
app.set('appPath', config.root + '/public'); | |
app.use(morgan('dev')); | |
} | |
if ('development' === env || 'test' === env) { | |
app.use(require('connect-livereload')()); | |
app.use(express.static(path.join(config.root, '.tmp'))); | |
app.use(express.static(path.join(config.root, 'client'))); | |
app.set('appPath', 'client'); | |
app.use(morgan('dev')); | |
app.use(errorHandler()); // Error handler - has to be last | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My personal favorite Javascript fullstack framework is https://github.com/DaftMonk/generator-angular-fullstack generated via Yeoman. Here I've included a copy of their Node.js application & Express framework configuration.