-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
58 lines (45 loc) · 1.31 KB
/
app.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
'use strict';
if (process.env.NEW_RELIC_LICENSE_KEY) require('newrelic');
var express = require('express');
var bodyParser = require('body-parser');
var nunjucks = require('nunjucks');
var config = require('./lib/config');
// Routes
var index = require('./routes/index');
var hook = require('./routes/hook');
var env;
// Set up Express
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Set up Nunjucks
env = nunjucks.configure('views', {
autoescape: true,
express: app
});
// Global template variables
env.addGlobal('brand', config.brand);
env.addGlobal('trackingID', config.trackingID);
env.addGlobal('NODE_ENV', process.env.NODE_ENV);
app.use('/', index);
app.use('/hook', hook);
// Catch 404s and forward them to the error handler
app.use(function(request, response, next) {
var error = new Error('Not Found');
error.status = 404;
next(error);
});
// Error handler
// The callback needs to have four arguments, even if some are unused, for
// Express to correctly identify it as error middleware.
app.use(function(error, request, response, next) {
var context = {
title: error.message,
message: error.message
};
response.status(error.status || 500);
response.render('error.html', context);
});
module.exports = app;