-
Notifications
You must be signed in to change notification settings - Fork 90
/
server.js
executable file
·85 lines (72 loc) · 2.49 KB
/
server.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
var path = require('path')
var express = require('express')
var nunjucks = require('nunjucks')
var enforce = require('express-sslify')
var routes = require('./app/routes.js')
var app = express()
var bodyParser = require('body-parser')
var port = (process.env.PORT || 3000)
var IS_HEROKU = process.env.hasOwnProperty('IS_HEROKU')
module.exports = app
if (IS_HEROKU) {
app.use(enforce.HTTPS({
// Heroku uses a reverse proxy for SSL, but then forwards traffic to the
// website over http. The X-Forwarded-Proto header provides the original
// protocol used, but we have to tell express-sslify to trust it.
trustProtoHeader: true
}))
}
// Application settings
app.set('view engine', 'html')
// Set the location of the views and govuk_template layout file
var appViews = [
path.join(__dirname, '/app/views'),
path.join(__dirname, '/node_modules/govuk_template_jinja/views/layouts')
]
// Tell nunjucks we are using express to serve the templates within
// the views defined in appViews
nunjucks.configure(appViews, {
express: app,
autoescape: true,
watch: true,
noCache: true
})
// Middleware to serve static assets
app.use('/public', express.static(path.join(__dirname, '/public')))
app.use('/public', express.static(path.join(__dirname, '/node_modules/govuk_template_jinja/assets')))
app.use('/public', express.static(path.join(__dirname, '/node_modules/govuk_frontend_toolkit')))
// Support for parsing data in POSTs
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
// send assetPath to all views
app.use(function (req, res, next) {
res.locals.asset_path = '/public/'
next()
})
// Hot patch send method to manipulate HTML before it is sent.
app.use(function (req, res, next) {
var send = res.send
res.send = function () {
// Check first argument is html before manipluating it
var isString = typeof arguments[0] === 'string'
var isHTML = isString && arguments[0].trim().startsWith('<!DOCTYPE html>')
if (isHTML) {
// Remove the cookie banner from the page so cookies are not set,
// this is not configurable in govuk_template.
var cookieBannerRegex = /<div id="global-cookie-message">\s*<\/div>/
arguments[0] = arguments[0].replace(cookieBannerRegex, '')
}
send.apply(this, arguments)
}
next()
})
// routes (found in routes.js)
routes.bind(app, '/public/')
// start the app
app.listen(port, function () {
if (!IS_HEROKU) {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
}
})