forked from dharFr/Node-JS-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.coffee
91 lines (67 loc) · 1.95 KB
/
startup.coffee
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
express = require 'express'
assets = require 'connect-assets'
mongoose = require 'mongoose'
connect = require 'connect'
app = module.exports = express.createServer()
# browserId Configuration
if process.env.HOST
AUDIENCE = process.env.HOST
else
AUDIENCE = "http://localhost:3000"
console.log "defined browserid AUDIENCE:", AUDIENCE
# mongodb config
mongoUrl = process.env.MONGOLAB_URI|| "mongdb://localhost:27017/db"
console.log "mongoUrl : #{mongoUrl}"
mongoose.connect(mongoUrl, (err) ->
if err then throw err
console.log 'Connected to DB'
)
# init routes
routes = require './routes'
console.log "Routes loaded"
publicDir = "#{__dirname}/public"
app.configure ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.use express.bodyParser()
app.use express.methodOverride()
app.use connect.cookieParser(process.env.SECRET || 'my hard to guess secret')
app.use connect.cookieSession(cookie: { maxAge: 30 * 60 * 1000 })
app.use connect.csrf()
app.use app.router
app.use express.static publicDir
app.use assets() #build: true
app.use (err, req, res, next) ->
if err.status
res.status(err.status)
if err.status == 403
res.sendfile "#{publicDir}/403.html"
else
next(err)
app.configure 'development', ->
app.use( express.errorHandler(
dumpExceptions: true
showStack: true
))
app.configure 'production', ->
app.use( express.errorHandler() )
# 404 Page
app.use (req, res, next) ->
res.status(404)
res.sendfile "#{publicDir}/404.html"
console.log "Configuring URLs"
# Routes
app.post '/auth', routes.auth.auth(AUDIENCE)
app.get '/logout', routes.auth.logout
needAuth = [routes.profile.loadUser, routes.profile.needUser]
app.get '/profile', needAuth, routes.profile.show
app.post '/profile', needAuth, routes.profile.save
app.get '/', routes.profile.loadUser, routes.main.index
app.helpers {
appName: 'Sample node app'
}
app.dynamicHelpers {
user: (req, res) -> req.user
_csrf: (req, res) -> req.session._csrf
}
exports = app