-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·273 lines (227 loc) · 7.3 KB
/
app.js
File metadata and controls
executable file
·273 lines (227 loc) · 7.3 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Core dependencies
const {
createReadStream,
createWriteStream,
existsSync,
mkdirSync
} = require('node:fs')
const { join } = require('node:path')
const { format: urlFormat } = require('node:url')
// External dependencies
const bodyParser = require('body-parser')
const sessionInCookie = require('client-sessions')
const cookieParser = require('cookie-parser')
const dotenv = require('dotenv')
const express = require('express')
const sessionInMemory = require('express-session')
const nunjucks = require('nunjucks')
// Run before other code to make sure variables from .env are available
dotenv.config({
quiet: true
})
// Local dependencies
const config = require('./app/config')
const locals = require('./app/locals')
const routes = require('./app/routes')
const exampleTemplatesRoutes = require('./lib/example_templates_routes')
const authentication = require('./lib/middleware/authentication')
const automaticRouting = require('./lib/middleware/auto-routing')
const production = require('./lib/middleware/production')
const prototypeAdminRoutes = require('./lib/middleware/prototype-admin-routes')
const utils = require('./lib/utils')
const packageInfo = require('./package.json')
// Set configuration variables
const port = parseInt(process.env.PORT || config.port, 10) || 2000
// Initialise applications
const app = express()
const exampleTemplatesApp = express()
// Set up configuration variables
const useAutoStoreData =
process.env.USE_AUTO_STORE_DATA || config.useAutoStoreData
const useCookieSessionStore =
process.env.USE_COOKIE_SESSION_STORE || config.useCookieSessionStore
// Add variables that are available in all views
app.locals.asset_path = '/public/'
app.locals.useAutoStoreData = useAutoStoreData === 'true'
app.locals.useCookieSessionStore = useCookieSessionStore === 'true'
app.locals.serviceName = config.serviceName
// Use cookie middleware to parse cookies
app.use(cookieParser())
// Nunjucks configuration for application
const appViews = [
join(__dirname, 'app/views/'),
join(__dirname, 'lib/example-templates/'),
join(__dirname, 'lib/prototype-admin/'),
join(__dirname, 'lib/templates/'),
join(__dirname, 'node_modules/nhsuk-frontend/dist/nhsuk/components'),
join(__dirname, 'node_modules/nhsuk-frontend/dist/nhsuk/macros'),
join(__dirname, 'node_modules/nhsuk-frontend/dist/nhsuk'),
join(__dirname, 'node_modules/nhsuk-frontend/dist')
]
/**
* @type {ConfigureOptions}
*/
const nunjucksConfig = {
autoescape: true,
noCache: true
}
nunjucksConfig.express = app
let nunjucksAppEnv = nunjucks.configure(appViews, nunjucksConfig)
nunjucksAppEnv.addGlobal('version', packageInfo.version)
// Add Nunjucks filters
utils.addNunjucksFilters(nunjucksAppEnv)
// Session uses service name to avoid clashes with other prototypes
const sessionName = `nhsuk-prototype-kit-${Buffer.from(config.serviceName, 'utf8').toString('hex')}`
const sessionOptions = {
secret: sessionName,
cookie: {
maxAge: 1000 * 60 * 60 * 4 // 4 hours
}
}
if (process.env.NODE_ENV === 'production') {
app.use(production)
app.use(authentication)
}
// Support session data in cookie or memory
if (useCookieSessionStore === 'true') {
app.use(
sessionInCookie({
...sessionOptions,
cookieName: sessionName,
proxy: true,
requestKey: 'session'
})
)
} else {
app.use(
sessionInMemory({
...sessionOptions,
name: sessionName,
resave: false,
saveUninitialized: false
})
)
}
// Support for parsing data in POSTs
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true
})
)
// Automatically store all data users enter
if (useAutoStoreData === 'true') {
app.use(utils.autoStoreData)
utils.addCheckedFunction(nunjucksAppEnv)
}
app.use(utils.setLocals)
// Warn if node_modules folder doesn't exist
function checkFiles() {
const nodeModulesExists = existsSync(join(__dirname, '/node_modules'))
if (!nodeModulesExists) {
throw new Error(
'ERROR: Node module folder missing. Try running `npm install`'
)
}
// Create template .env file if it doesn't exist
const envExists = existsSync(join(__dirname, '/.env'))
if (!envExists) {
createReadStream(join(__dirname, '/lib/template.env')).pipe(
createWriteStream(join(__dirname, '/.env'))
)
}
}
// initial checks
checkFiles()
// Create template session data defaults file if it doesn't exist
const dataDirectory = join(__dirname, '/app/data')
const sessionDataDefaultsFile = join(dataDirectory, '/session-data-defaults.js')
const sessionDataDefaultsFileExists = existsSync(sessionDataDefaultsFile)
if (!sessionDataDefaultsFileExists) {
console.log('Creating session data defaults file')
if (!existsSync(dataDirectory)) {
mkdirSync(dataDirectory)
}
createReadStream(
join(__dirname, '/lib/template.session-data-defaults.js')
).pipe(createWriteStream(sessionDataDefaultsFile))
}
// Local variables
app.use(locals(config))
// View engine
app.set('view engine', 'html')
exampleTemplatesApp.set('view engine', 'html')
// Support for parsing nested query strings
// https://github.com/nhsuk/nhsuk-prototype-kit/issues/644
app.set('query parser', 'extended')
// This setting trusts the X-Forwarded headers set by
// a proxy and uses them to set the standard header in
// req. This is needed for hosts like Heroku.
// See https://expressjs.com/en/guide/behind-proxies.html
app.set('trust proxy', 1)
// Use public folder for static assets
app.use(express.static(join(__dirname, 'public')))
// Use assets from NHS frontend
app.use(
'/nhsuk-frontend',
express.static(join(__dirname, 'node_modules/nhsuk-frontend/dist/nhsuk'))
)
// Use custom application routes
app.use('/', routes)
// Automatically route pages
app.get(/^([^.]+)$/, (req, res, next) => {
automaticRouting.matchRoutes(req, res, next)
})
// Example template routes
app.use('/example-templates', exampleTemplatesApp)
nunjucksAppEnv = nunjucks.configure(appViews, {
autoescape: true,
express: exampleTemplatesApp
})
nunjucksAppEnv.addGlobal('version', packageInfo.version)
// Add Nunjucks filters
utils.addNunjucksFilters(nunjucksAppEnv)
exampleTemplatesApp.use('/', exampleTemplatesRoutes)
// Automatically route example template pages
exampleTemplatesApp.get(/^([^.]+)$/, (req, res, next) => {
automaticRouting.matchRoutes(req, res, next)
})
app.use('/prototype-admin', prototypeAdminRoutes)
// Redirect all POSTs to GETs - this allows users to use POST for autoStoreData
app.post(/^\/([^.]+)$/, (req, res) => {
res.redirect(
urlFormat({
pathname: `/${req.params[0]}`,
query: req.query
})
)
})
// Catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error(`Page not found: ${req.path}`)
err.status = 404
next(err)
})
// Display error
app.use((err, req, res) => {
console.error(err.message)
res.status(err.status || 500)
res.send(err.message)
})
// Run the application
app.listen(port)
if (
process.env.WATCH !== 'true' && // If the user isn’t running watch
process.env.NODE_ENV !== 'production' // and it’s not in production mode
) {
console.info(`Running at http://localhost:${port}/`)
console.info('')
console.warn(
'Warning: It looks like you may have run the command `npm start` locally.'
)
console.warn('Press `Ctrl+C` and then run `npm run watch` instead')
}
module.exports = app
/**
* @import { ConfigureOptions } from 'nunjucks'
*/