Permalink
Browse files

use middleware for view helpers and cleanup config file

  • Loading branch information...
1 parent 32915f0 commit 65c2864f642a6eb57eb2a57903a643fa594e24ed @madhums committed Dec 25, 2012
Showing with 84 additions and 42 deletions.
  1. +73 −0 app/helpers/view.js
  2. +4 −0 config/config.example.js
  3. +7 −42 config/express.js
View
@@ -0,0 +1,73 @@
+
+/**
+ * Pagination helper
+ *
+ * @param {Number} pages
+ * @param {Number} page
+ * @return {String}
+ * @api private
+ */
+
+function createPagination (req) {
+ return function createPagination (pages, page) {
+ var url = require('url')
+ , qs = require('querystring')
+ , params = qs.parse(url.parse(req.url).query)
+ , str = ''
+
+ params.page = 0
+ var clas = page == 0 ? "active" : "no"
+ str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">First</a></li>'
+ for (var p = 1; p < pages; p++) {
+ params.page = p
+ clas = page == p ? "active" : "no"
+ str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">'+ p +'</a></li>'
+ }
+ params.page = --p
+ clas = page == params.page ? "active" : "no"
+ str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">Last</a></li>'
+
+ return str
+ }
+}
+
+/**
+ * Format date helper
+ *
+ * @param {Date} date
+ * @return {String}
+ * @api private
+ */
+
+function formatDate (date) {
+ var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" ]
+ return monthNames[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear()
+}
+
+/**
+ * Strip script tags
+ *
+ * @param {String} str
+ * @return {String}
+ * @api private
+ */
+
+function stripScript (str) {
+ return str.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
+}
+
+
+module.exports = function (config) {
+ return function (req, res, next) {
+ res.locals.appName = config.app.name
+ res.locals.title = 'Nodejs Express Mongoose Demo'
+ res.locals.req = req
+ res.locals.isActive = function (link) {
+ return req.url === link ? 'active' : ''
+ }
+ res.locals.formatDate = formatDate
+ res.locals.stripScript = stripScript
+ res.locals.createPagination = createPagination(req)
+ next()
+ }
+}
View
@@ -1,6 +1,10 @@
module.exports = {
development: {
+ root: require('path').normalize(__dirname + '/..'),
+ app: {
+ name: 'Nodejs Express Mongoose Demo'
+ },
db: 'mongodb://localhost/noobjs_dev',
facebook: {
clientID: "APP_ID"
View
@@ -6,6 +6,7 @@
var express = require('express')
, mongoStore = require('connect-mongodb')
, flash = require('connect-flash')
+ , viewHelpers = require('../app/helpers/view')
exports.boot = function(app, config, passport){
bootApplication(app, config, passport)
@@ -16,52 +17,16 @@ exports.boot = function(app, config, passport){
function bootApplication(app, config, passport) {
app.set('showStackError', true)
-
- app.use(express.static(__dirname + '/public'))
-
+ app.use(express.static(config.root + '/public'))
app.use(express.logger(':method :url :status'))
// set views path, template engine and default layout
- app.set('views', __dirname + '/app/views')
+ app.set('views', config.root + '/app/views')
app.set('view engine', 'jade')
app.configure(function () {
// dynamic helpers
- app.use(function (req, res, next) {
- res.locals.appName = 'Nodejs Express Mongoose Demo'
- res.locals.title = 'Nodejs Express Mongoose Demo'
- res.locals.showStack = app.showStackError
- res.locals.req = req
- res.locals.formatDate = function (date) {
- var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" ]
- return monthNames[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear()
- }
- res.locals.stripScript = function (str) {
- return str.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
- }
- res.locals.createPagination = function (pages, page) {
- var url = require('url')
- , qs = require('querystring')
- , params = qs.parse(url.parse(req.url).query)
- , str = ''
-
- params.page = 0
- var clas = page == 0 ? "active" : "no"
- str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">First</a></li>'
- for (var p = 1; p < pages; p++) {
- params.page = p
- clas = page == p ? "active" : "no"
- str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">'+ p +'</a></li>'
- }
- params.page = --p
- clas = page == params.page ? "active" : "no"
- str += '<li class="'+clas+'"><a href="?'+qs.stringify(params)+'">Last</a></li>'
-
- return str
- }
-
- next()
- })
+ app.use(viewHelpers(config))
// cookieParser should be above session
app.use(express.cookieParser())
@@ -70,6 +35,7 @@ function bootApplication(app, config, passport) {
app.use(express.bodyParser())
app.use(express.methodOverride())
+ // express/mongo session storage
app.use(express.session({
secret: 'noobjs',
store: new mongoStore({
@@ -78,8 +44,10 @@ function bootApplication(app, config, passport) {
})
}))
+ // connect flash for flash messages
app.use(flash())
+ // use passport session
app.use(passport.initialize())
app.use(passport.session())
@@ -109,7 +77,4 @@ function bootApplication(app, config, passport) {
})
})
-
- app.set('showStackError', false)
-
}

0 comments on commit 65c2864

Please sign in to comment.