-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.coffee
89 lines (79 loc) · 2.36 KB
/
web.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
# Web framework
express = require 'express'
# Templating language
jade = require 'jade'
# Dynamic CSS
stylus = require 'stylus'
# String validation
validator = require 'validator'
# Misc. utils
_ = require 'underscore'
# Logger
winston = require 'winston'
# Configuration management
nconf = require 'nconf'
# Configure 'nconf'
nconf.argv().env()
# Environment specific configs
if process.env.NODE_ENV? and process.env.NODE_ENV != ""
nconf.add 'user',
type:'file'
file: './configs/' + process.env.NODE_ENV + '.json'
# Default config
nconf.add 'global',
type:'file'
file: './configs/defaults.json'
# Set winston to write logs to file if 'logFile' is defined
if nconf.get('logFile')?
winston.add winston.transports.File, { filename: nconf.get('logFile') }
winston.remove winston.transports.Console
# Setup express
app = express()
# Don't forget the favicons!
app.use express.favicon('static/favicon.ico')
# If cookies are used, encrypt them!
app.use express.cookieParser(nconf.get('cookieSecret'))
# Session vars are nice, but personally not a fan.
# Try to use Redis, memcached, or other independant solutions.
#app.use express.cookieSession()
# Parse body
app.use express.bodyParser()
# asset pipeline
app.use require('connect-assets')()
# Static files (on prod, have web servers serve these)
app.use '/static', express.static __dirname + '/static'
# Setup jade template views
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.set 'view options', layout: false
# Set locals for global template context
app.use (req,res,next) ->
res.locals.env = process.env.NODE_ENV
next()
# Landing page
app.get '/', (req, res, next) ->
res.render 'pages/landing'
# Demonstrate unhandled exceptions
app.get '/throw', (req, res, next) ->
throw new Error('handles exceptions!')
# Demonstrate next(err)
app.get '/next_err', (req, res, next) ->
err = new Error('something happened')
err.extra_data =
foo: 'bar'
bar: 'foo'
err.status = 503
next err
# Not found if reached here
app.use (req, res, next) ->
res.status 404
res.render '404'
# Error Handling
app.use (err, req, res, next) ->
res.status(err.status || 500)
res.render '500'
# Log stuff
winston.error err.message, {"module": "web", "error": err, "headers": req.headers}
# Start Web Server
app.listen nconf.get('port'), ->
winston.info "Server is up and running!", { "port" : nconf.get('port') }