forked from madhums/node-express-mongoose-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
112 lines (86 loc) · 3.1 KB
/
settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Module dependencies.
*/
var express = require('express')
, mongoStore = require('connect-mongodb')
exports.boot = function(app, config, passport){
bootApplication(app, config, passport)
}
// App settings and middleware
function bootApplication(app, config, passport) {
app.set('showStackError', true)
app.use(express.static(__dirname + '/public'))
app.use(express.logger(':method :url :status'))
// set views path, template engine and default layout
app.set('views', __dirname + '/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()
})
// cookieParser should be above session
app.use(express.cookieParser())
// bodyParser should be above methodOverride
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.session({
secret: 'noobjs',
store: new mongoStore({
url: config.db,
collection : 'sessions'
})
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(express.favicon())
// routes should be at the last
app.use(app.router)
// assume "not found" in the error msgs
// is a 404. this is somewhat silly, but
// valid, you can do whatever you like, set
// properties, use instanceof etc.
app.use(function(err, req, res, next){
// treat as 404
if (~err.message.indexOf('not found')) return next()
// log it
console.error(err.stack)
// error page
res.status(500).render('500')
})
// assume 404 since no middleware responded
app.use(function(req, res, next){
res.status(404).render('404', { url: req.originalUrl })
})
})
app.set('showStackError', false)
}