Skip to content

Instantly share code, notes, and snippets.

@timstermatic
Last active December 21, 2015 08:39
Show Gist options
  • Save timstermatic/6279983 to your computer and use it in GitHub Desktop.
Save timstermatic/6279983 to your computer and use it in GitHub Desktop.
app.js for simple MVC
// app.js for simple express mvc
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
var fs = require('fs');
var mongoose = require('mongoose');
mongoose.connect('mongodb://locahost/dbname');
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.set('layout',false)
// CONTROLLER
fs.readdirSync('./controllers').forEach(function (file) {
if(file.substr(-3) == '.js') {
route = require('./controllers/' + file);
route.controller(app);
}
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment