Last active
December 12, 2015 08:59
-
-
Save dunst0/4747798 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, http = require('http') | |
, path = require('path') | |
, fs = require('fs') | |
, redis = require('redis'); | |
var app = express(); | |
var db = redis.createClient(); | |
app.configure(function() { | |
app.set('port', process.env.PORT || 8080); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.set('controllers', 'controllers'); | |
app.set('defaultController', 'home'); | |
app.set('database', db); | |
app.locals.title = 'My App'; | |
app.use(express.logger('dev')); | |
app.use(express.favicon()); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser('your secret here')); //TODO: change | |
//app.use(express.session()); //set to redis! | |
app.use(function(req, res, next) { | |
var authcookie = req.cookies.auth; | |
if (authcookie == undefined) return next(); | |
db.get('auth:' + authcookie, function(err, userid) { | |
if (err) return next(err); | |
if (userid == null) return next(); | |
db.get('uid:' + userid + ':auth', function(err, auth) { | |
if (err) return next(err); | |
if (auth == null) return next(); | |
req.authorized = authcookie == auth; | |
req.user = { "userid": userid }; | |
next(); | |
}); | |
}); | |
}); | |
app.use(app.router); | |
app.use(require('stylus').middleware(__dirname + '/public')); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
}); | |
app.configure('development', function() { | |
app.use(express.errorHandler()); | |
}); | |
/*----------------------------------------------------------------------------*/ | |
/* defining routes */ | |
app.get('/', function(req, res) { | |
res.redirect('/' + app.get('defaultController')); | |
}); | |
fs.readdirSync(__dirname + '/' + app.get('controllers')).forEach(function(name) { | |
var controller = require('./' + app.get('controllers') + '/' + name); | |
app.use('/' + controller.name, controller(app)); | |
}); | |
/*----------------------------------------------------------------------------*/ | |
/* start server */ | |
http.createServer(app).listen(app.get('port'), function() { | |
console.log('Express server listening on port ' + app.get('port')); | |
}); |
This file contains 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
function home(parent) { | |
var express = require('express'); | |
var app = express(); | |
app.locals = parent.locals; | |
app.get('/(index)?', function(req, res) { | |
var user = req.user || {}; | |
res.render('home/index'); | |
}); | |
app.get('/imprint', function(req, res) { | |
res.send('home:imprint'); | |
}); | |
return app; | |
} | |
module.exports = home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment