Created
February 13, 2013 06:59
-
-
Save yalamber/4942790 to your computer and use it in GitHub Desktop.
app.js
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
var express = require('express') | |
, S = require('string') | |
, moment = require('moment') | |
, redis = require('redis') | |
, RedisStore = require('connect-redis')(express) | |
, rClient = exports.rClient = redis.createClient() | |
, sessionStore = exports.sessionStore = new RedisStore({client: rClient}) | |
, app = express() | |
, cfg = require('./config/app_conf') | |
, passport = require('passport') | |
, TwitterStrategy = require('passport-twitter').Strategy | |
, mysqlClient = require('mysql') | |
, dbConnection = mysqlClient.createConnection({ | |
host : cfg.db_host, | |
user : cfg.db_user, | |
password : cfg.db_pass, | |
database : cfg.db_name | |
}) | |
, controllers = require('./system/controllers')({ | |
app : app, | |
dbConnection : dbConnection, | |
cfg : cfg, | |
S : S, | |
moment : moment | |
}); | |
//locals | |
app.locals({ | |
site_name: cfg.site_name, | |
site_url: cfg.site_url, | |
main_menu: cfg.main_menu, | |
twitter_username: cfg.twitter.username, | |
title: '', | |
S: S, | |
admin_panel: false | |
}); | |
//authorize redis | |
rClient.auth(cfg.redis.pass, function (err) { if (err) throw err; }); | |
app.configure(function(){ | |
//views | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'ejs'); | |
//parse request bodies | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
// session support | |
app.use(express.cookieParser(cfg.cookie_secret)); | |
app.use(express.session({ | |
key : 'whynode', | |
store : sessionStore, | |
cookie: { | |
expires: new Date(Date.now() + 60 * 10000), | |
maxAge: 60*10000 | |
} | |
})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
//pass user data | |
app.use(function(req, res, next) { | |
res.locals.req_path = req.path; | |
res.locals.user = req.user || false; | |
next(); | |
}); | |
//get routers | |
app.use(app.router); | |
//serve asset files | |
app.use('/assets', express.static(__dirname + '/public')); | |
}); | |
//Passportjs auth strategy | |
require('./strategy'); | |
//dev environment | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
//production environment | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
}); | |
//routes | |
require('./config/route')(app, controllers, passport); | |
//404 error | |
app.use(function(req, res, next){ | |
res.status(404); | |
// respond with html page | |
if (req.accepts('html')) { | |
res.render('404', { url: req.url, title: 'Not found',body: 'Page not found' }); | |
return; | |
} | |
// respond with json | |
if (req.accepts('json')) { | |
res.send({ error: 'Not found' }); | |
return; | |
} | |
// default to plain-text. send() | |
res.type('txt').send('Not found'); | |
}); | |
app.use(function(err, req, res, next){ | |
console.error(err.stack); | |
res.send(500, 'Something broke!'); | |
}); | |
app.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment