This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
164 lines (140 loc) · 4.47 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require('dotenv').config()
const globalError = require('http-errors')
// import node modules.
const express = require('express'),
cookieParser = require('cookie-parser'),
trimRequest = require('trim-request'),
compression = require('compression'),
helmet = require('helmet'),
morgan = require('morgan'),
morganConfig = require('./config/morgan.config'),
sassMiddleware = require('node-sass-middleware'),
path = require('path'),
sessionConfig = require('./config/session.config'),
csp = require('./config/csp.config'),
{
SINFilter,
hasData,
checkPublic,
sortByLineNumber,
checkLangQuery,
currencyFilter,
isoDateHintText,
currencyWithoutUnit,
is65,
} = require('./utils'),
csrf = require('csurf'),
cookieConfig = require('./config/cookie.config'),
rateLimit = require('express-rate-limit')
// initialize application.
var app = express()
// view engine setup
app.set('views', path.join(__dirname, './views'))
app.set('view engine', 'pug')
// general app configuration.
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser(process.env.app_session_secret))
app.use(require('./config/i18n.config').init)
// CSRF setup
app.use(
csrf({
cookie: true,
signed: true,
...cookieConfig,
}),
)
// append csrfToken to all responses
app.use(function (req, res, next) {
res.locals.csrfToken = req.csrfToken()
next()
})
// set up rate limiter: maximum of five requests per minute
var limiter = new rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 120,
})
// apply rate limiter to expensive request page(s) - just the one for now
app.use('/login/dateOfBirth', limiter)
// in production we may want to use other than memorysession
app.use(sessionConfig)
// in production: precompile CSS
app.use(
sassMiddleware({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
debug: false,
indentedSyntax: false, // look for .scss files, not .sass files
sourceMap: true,
outputStyle: 'compressed',
}),
)
// public assets go here (css, js, etc)
app.use(express.static(path.join(__dirname, 'public')))
// add a request logger
process.env.NODE_ENV !== 'test' && app.use(morgan(morganConfig))
// dnsPrefetchControl controls browser DNS prefetching
// frameguard to prevent clickjacking
// hidePoweredBy to remove the X-Powered-By header
// hsts for HTTP Strict Transport Security
// ieNoOpen sets X-Download-Options for IE8+
// noSniff to keep clients from sniffing the MIME type
// xssFilter adds some small XSS protections
app.use(helmet())
app.use(helmet.contentSecurityPolicy({ directives: csp }))
// gzip response body compression.
app.use(compression())
app.use(trimRequest.all)
app.use(checkPublic)
app.use(checkLangQuery)
// on Heroku, redirect from the http url to https
app.use(function (req, res, next) {
if (
req.headers['host'] === 'claim-tax-benefits.herokuapp.com' &&
req.headers['x-forwarded-proto'] === 'http'
) {
return res.redirect('https://' + req.headers.host + req.url)
}
next()
})
// Adding values/functions to app.locals means we can access them in our templates
app.locals.GITHUB_SHA = process.env.GITHUB_SHA || null
app.locals.SINFilter = SINFilter
app.locals.currencyWithoutUnit = currencyWithoutUnit
app.locals.hasData = hasData
app.locals.currencyFilter = currencyFilter
app.locals.sortByLineNumber = sortByLineNumber
app.locals.isoDateHintText = isoDateHintText
app.locals.is65 = is65
// configure routes
require('./routes/start/start.controller')(app)
require('./routes/login/login.controller')(app)
require('./routes/personal/personal.controller')(app)
require('./routes/deductions/deductions.controller')(app)
require('./routes/vote/vote.controller')(app)
require('./routes/confirmation/confirmation.controller')(app)
require('./routes/offramp/offramp.controller')(app)
require('./routes/cancel/cancel.controller')(app)
// clear session
app.get('/clear', (req, res) => {
req.session.destroy()
res.redirect(302, '/')
})
app.use(function (req, res, next) {
next(globalError(404))
})
// Pass error information to res.locals
app.use((err, req, res, next) => {
let errObj = {}
let status = err.status || err.statusCode || 500
res.statusCode = status
errObj.status = status
if (err.message) errObj.message = err.message
if (err.stack) errObj.stack = err.stack
if (err.code) errObj.code = err.code
if (err.name) errObj.name = err.name
if (err.type) errObj.type = err.type
res.locals.err = errObj
next(err)
})
module.exports = app