-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.js
executable file
·254 lines (205 loc) · 7.45 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
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
// Core dependencies
const path = require('path');
// External dependencies
const browserSync = require('browser-sync');
const compression = require('compression');
const express = require('express');
const helmet = require('helmet');
const highlightjs = require('highlight.js');
const nunjucks = require('nunjucks');
// Local dependencies
const authentication = require('./middleware/authentication');
const config = require('./app/config');
const fileHelper = require('./middleware/file-helper');
const locals = require('./app/locals');
const routing = require('./middleware/routing');
const PageIndex = require('./middleware/page-index');
const pageIndex = new PageIndex(config);
// Initialise applications
const app = express();
// Authentication middleware
app.use(authentication);
// Use local variables
app.use(locals(config));
// Use gzip compression to decrease the size of
// the response body and increase the speed of web app
app.use(compression());
// Use helmet to help secure the application
// by setting http headers
app.use(
helmet({
contentSecurityPolicy: false,
}),
);
// Middleware to serve static assets
app.use(express.static(path.join(__dirname, 'public')));
app.use('/nhsuk-frontend', express.static(path.join(__dirname, '/node_modules/nhsuk-frontend/dist')));
app.use('/nhsuk-frontend', express.static(path.join(__dirname, '/node_modules/nhsuk-frontend/packages')));
app.use('/iframe-resizer', express.static(path.join(__dirname, 'node_modules/iframe-resizer/')));
// View engine (nunjucks)
app.set('view engine', 'njk');
// Nunjucks configuration
const appViews = [
path.join(__dirname, '/app/views/'),
path.join(__dirname, '/node_modules/nhsuk-frontend/packages/components'),
path.join(__dirname, '/node_modules/nhsuk-frontend/packages/macros'),
];
const env = nunjucks.configure(appViews, {
autoescape: true,
express: app,
noCache: true,
watch: true,
});
/*
* Add some global nunjucks helpers
*/
env.addGlobal('getHTMLCode', fileHelper.getHTMLCode);
env.addGlobal('getNunjucksCode', fileHelper.getNunjucksCode);
env.addGlobal('getJSONCode', fileHelper.getJSONCode);
env.addFilter('highlight', (code, language) => {
const languages = language ? [language] : false;
return highlightjs.highlightAuto(code.trim(), languages).value;
});
// Render standalone design examples
app.get('/design-example/:group/:item/:type', (req, res) => {
const displayFullPage = req.query.fullpage === 'true';
const blankPage = req.query.blankpage === 'true';
const { group } = req.params;
const { item } = req.params;
const { type } = req.params;
const examplePath = path.join(__dirname, `app/views/design-system/${group}/${item}/${type}/index.njk`);
// Get the given example as HTML.
const exampleHtml = fileHelper.getHTMLCode(examplePath);
// Wrap the example HTML in a basic html base template.
let baseTemplate = 'includes/design-example-wrapper.njk';
if (displayFullPage) {
baseTemplate = 'includes/design-example-wrapper-full.njk';
}
if (blankPage) {
baseTemplate = 'includes/design-example-wrapper-blank.njk';
}
res.render(baseTemplate, {
body: exampleHtml,
item,
});
});
app.get('/search', (req, res) => {
const query = req.query['search-field'] || '';
const resultsPerPage = 10;
let currentPage = parseInt(req.query.page, 10);
const results = pageIndex.search(query);
const maxPage = Math.ceil(results.length / resultsPerPage);
if (!Number.isInteger(currentPage)) {
currentPage = 1;
} else if (currentPage > maxPage || currentPage < 1) {
currentPage = 1;
}
const startingIndex = resultsPerPage * (currentPage - 1);
const endingIndex = startingIndex + resultsPerPage;
res.render('includes/search.njk', {
currentPage,
maxPage,
query,
results: results.slice(startingIndex, endingIndex),
resultsLen: results.length,
});
});
app.get('/suggestions', (req, res) => {
const results = pageIndex.search(req.query.search);
const slicedResults = results.slice(0, 10);
res.set({ 'Content-Type': 'application/json' });
res.send(JSON.stringify(slicedResults));
});
app.get('/assets/NHS_design_principles.pdf', (req, res) => {
res.redirect('/assets/nhs-design-principles.pdf');
});
app.get('/slack', (req, res) => {
res.redirect('https://join.slack.com/t/nhs-service-manual/shared_invite/enQtNTIyOTEyNjU3NDkyLTk4NDQ3YzkwYzk1Njk5YjAxYTI5YTVkZmUxMGQ0ZjA3NjMyM2ZkNjBlMWMxODVjZjYzNzg1ZmU4MWY1NmE2YzE');
});
// Add the code redirects for community-and-contribution pages
app.get('/community/contribution-survey', (req, res) => {
res.redirect('https://nhsdigital.eu.qualtrics.com/jfe/form/SV_5szVfoxZIW7Kr1b');
});
app.get('/community/backlog-of-components-and-patterns', (req, res) => {
res.redirect('/community-and-contribution/backlog-of-components-and-patterns');
});
app.get('/community/contribution-criteria', (req, res) => {
res.redirect('/community-and-contribution/contribution-criteria');
});
app.get('/community/develop-component-pattern', (req, res) => {
res.redirect('/community-and-contribution/develop-component-pattern');
});
app.get('/community', (req, res) => {
res.redirect('/community-and-contribution');
});
app.get('/community/propose-component-pattern', (req, res) => {
res.redirect('/community-and-contribution/propose-component-pattern');
});
// Redirects for new service standards URLs
app.get('/service-standard', (req, res) => {
res.redirect(301, '/standards-and-technology/service-standard');
});
app.get('/service-standard/about', (req, res) => {
res.redirect(301, '/standards-and-technology/about-the-service-standard');
});
app.get('/service-standard/:page', (req, res) => {
res.redirect(301, `/standards-and-technology/service-standard-points/${req.params.page}`);
});
// Redirects for accessibility URLs
app.get('/accessibility/new-accessibility-requirements-wcag-2-2', (req, res) => {
res.redirect('/accessibility/new-criteria-in-wcag-2-2');
});
// Redirects for design system patterns
app.get('/design-system/patterns/ask-users-for-their-nhs-number', (req, res) => {
res.redirect('/design-system/patterns/ask-for-nhs-numbers');
});
app.get('/design-system/patterns/reassure-users-that-a-page-is-up-to-date', (req, res) => {
res.redirect('/design-system/patterns/know-that-a-page-is-up-to-date');
});
// REDIRECT STOPS HERE
app.get('/content/health-literacy/use-a-readability-tool-to-prioritise-content', (req, res) => {
res.redirect(302, '/page-not-found');
});
// PDF page redirect
// https://github.com/nhsuk/nhsuk-service-manual/pull/965
app.get('/content/pdfs', (req, res) => {
res.redirect('/content/pdfs-and-other-non-html-documents');
});
// Automatically route pages
app.get(/^([^.]+)$/, (req, res, next) => {
routing.matchRoutes(req, res, next);
});
// Render sitemap.xml in XML format
app.get('/sitemap.xml', (_, res) => {
res.set({ 'Content-Type': 'application/xml' });
res.render('sitemap.xml');
});
// Render robots.txt in text format
app.get('/robots.txt', (_, res) => {
res.set('text/plain');
res.render('robots.txt');
});
// Render 404 page
app.get('*', (_, res) => {
res.statusCode = 404;
res.render('page-not-found');
});
// Run application on configured port
if (config.env === 'development') {
app.listen(config.port - 50, () => {
browserSync({
files: ['app/views/**/*.*', 'public/**/*.*'],
notify: true,
open: false,
port: config.port,
proxy: `localhost:${config.port - 50}`,
ui: false,
});
});
} else {
app.listen(config.port);
}
setTimeout(() => {
pageIndex.init();
}, 2000);
module.exports = app;