This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
196 lines (160 loc) · 5.51 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
// 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');
const markdown = require('nunjucks-markdown');
const marked = require('marked');
// 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 { getRandomValues } = require('crypto');
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,
}),
);
app.use(express.static(path.join(__dirname, '/public')));
app.use('/dfeuk-frontend', express.static(path.join(__dirname, '/node_modules/dfeuk-frontend/dist')));
app.use('/dfeuk-frontend', express.static(path.join(__dirname, '/node_modules/dfeuk-frontend/packages')));
app.use('/dfe-frontend-alpha', express.static(path.join(__dirname, '/node_modules/dfe-frontend-alpha/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/dfeuk-frontend/packages/components'),
path.join(__dirname, '/node_modules/dfe-frontend-alpha/packages/components'),
];
const env = nunjucks.configure(appViews, {
autoescape: true,
express: app,
noCache: true,
watch: true,
});
markdown.register(env, marked.parse);
/*
* 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;
});
var addNunjucksFilters = function (env) {
var customFilters = require('./app/filters.js')(env)
var filters = Object.assign(customFilters)
Object.keys(filters).forEach(function (filterName) {
env.addFilter(filterName, filters[filterName])
})
}
addNunjucksFilters(env)
// 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) => {
console.log(req.query['search-field'])
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));
});
// 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;