-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·180 lines (144 loc) · 5.35 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
// Core dependencies
const path = require('path');
const fs = require('fs');
// External dependencies
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const dotenv = require('dotenv');
const express = require('express');
const nunjucks = require('nunjucks');
const sessionInMemory = require('express-session');
// Run before other code to make sure variables from .env are available
dotenv.config();
// Local dependencies
const packageInfo = require('./package.json');
const automaticRouting = require('./middleware/auto-routing');
const config = require('./app/config');
const locals = require('./app/locals');
const routes = require('./app/routes');
const utils = require('./lib/utils');
// Set configuration variables
const port = parseInt(process.env.PORT, 10) || config.port;
// Initialise applications
const app = express();
// Set up configuration variables
const useAutoStoreData = process.env.USE_AUTO_STORE_DATA || config.useAutoStoreData;
// Add variables that are available in all views
app.locals.asset_path = '/public/';
app.locals.useAutoStoreData = (useAutoStoreData === 'true');
app.locals.serviceName = config.serviceName;
// Use cookie middleware to parse cookies
app.use(cookieParser());
if (process.env.NODE_ENV === 'production') {
app.use((req, res, next) => {
// Set Strict-Transport-Security header to
// ensure that browsers only use HTTPS
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
next();
});
}
// Nunjucks configuration for application
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 nunjucksConfig = {
autoescape: true,
noCache: true,
};
nunjucksConfig.express = app;
const 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
},
};
// Support session data in memory
app.use(sessionInMemory(Object.assign(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);
}
// Warn if node_modules folder doesn't exist
function checkFiles() {
const nodeModulesExists = fs.existsSync(path.join(__dirname, '/node_modules'));
if (!nodeModulesExists) {
console.error('ERROR: Node module folder missing. Try running `npm install`'); // eslint-disable-line no-console
process.exit(0);
}
// Create template .env file if it doesn't exist
const envExists = fs.existsSync(path.join(__dirname, '/.env'));
if (!envExists) {
fs.createReadStream(path.join(__dirname, '/lib/template.env'))
.pipe(fs.createWriteStream(path.join(__dirname, '/.env')));
}
}
// initial checks
checkFiles();
// Create template session data defaults file if it doesn't exist
const dataDirectory = path.join(__dirname, '/app/data');
const sessionDataDefaultsFile = path.join(dataDirectory, '/session-data-defaults.js');
const sessionDataDefaultsFileExists = fs.existsSync(sessionDataDefaultsFile);
if (!sessionDataDefaultsFileExists) {
console.log('Creating session data defaults file'); // eslint-disable-line no-console
if (!fs.existsSync(dataDirectory)) {
fs.mkdirSync(dataDirectory);
}
fs.createReadStream(path.join(__dirname, '/lib/template.session-data-defaults.js'))
.pipe(fs.createWriteStream(sessionDataDefaultsFile));
}
// Local variables
app.use(locals(config));
// View engine
app.set('view engine', 'html');
// 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/packages')));
app.use('/nhsuk-frontend', express.static(path.join(__dirname, 'node_modules/nhsuk-frontend/dist')));
// Use custom application routes
app.use('/', routes);
// Automatically route pages
app.get(/^([^.]+)$/, (req, res, next) => {
automaticRouting.matchRoutes(req, res, next);
});
// Clear all data in session if you open /examples/passing-data/clear-data
app.post('/examples/passing-data/clear-data', (req, res) => {
req.session.data = {};
res.render('examples/passing-data/clear-data-success');
});
// Redirect all POSTs to GETs - this allows users to use POST for autoStoreData
app.post(/^\/([^.]+)$/, (req, res) => {
res.redirect(`/${req.params[0]}`);
});
// Catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error(`Page not found: ${req.path}`); // eslint-disable-line no-console
err.status = 404;
next(err);
});
// Display error
app.use((err, req, res) => {
console.error(err.message); // eslint-disable-line no-console
res.status(err.status || 500);
res.send(err.message);
});
// Run the application
app.listen(port);
module.exports = app;