-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
201 lines (175 loc) · 4.74 KB
/
server.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
import express from 'express';
import bodyParser from 'body-parser';
import nunjucks from 'nunjucks';
import path from 'path';
import edt from 'express-debug';
import {
sessionData,
addCheckedFunction,
matchRoutes,
addNunjucksFilters,
forceHttps
} from './utilities';
import routes from './api/routes';
import config from './app/config.js';
const dotenv = require('dotenv');
dotenv.config();
import helmet from 'helmet';
import sessionInMemory from 'express-session';
import { NONAME } from 'dns';
const app = express();
const PORT = process.env.PORT || config.PORT;
// Global vars
app.locals.serviceName = config.SERVICE_NAME;
// Local vars
const env = process.env.NODE_ENV;
if (env === 'production') {
app.use(helmet());
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'", 'unpkg.com', 'cdnjs.cloudflare.com'],
scriptSrc: [
"'self'",
"'sha256-+6WnXIl4mbFTCARd8N3COQmT3bJJmo32N8q8ZSQAIcU='",
'unpkg.com',
'cdnjs.cloudflare.com',
],
styleSrc: [
"'self'",
'cdn.jsdelivr.net',
'cdnjs.cloudflare.com',
'unpkg.com',
],
imgSrc: [
"'self'",
'data:',
'*.tile.openstreetmap.org',
'cdnjs.cloudflare.com',
'unpkg.com',
process.env.AZURE_BLOB_IMAGE_URL,
],
objectSrc: ["'none'"],
upgradeInsecureRequests: [],
},
})
);
}
let useHttps = process.env.USE_HTTPS || config.USE_HTTPS;
useHttps = useHttps.toLowerCase();
// Production session data
const session = require('express-session');
const AzureTablesStoreFactory = require('connect-azuretables')(session);
const isSecure = env === 'production' && useHttps === 'true';
if (isSecure) {
app.use(forceHttps);
}
// Support for parsing data in POSTs
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.set('trust proxy', 1); // needed for secure cookies on heroku
// Configure nunjucks environment
const nunjucksAppEnv = nunjucks.configure(
[
path.join(__dirname, './node_modules/govuk-frontend/'),
path.join(__dirname, './app/views/'),
],
{
autoescape: false,
express: app,
watch: env === 'development' ? true : false,
}
);
addCheckedFunction(nunjucksAppEnv);
addNunjucksFilters(nunjucksAppEnv);
// Set views engine
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, './dist')));
app.use('/uploads', express.static('uploads'));
app.use(
'/assets',
express.static(
path.join(__dirname, './node_modules/govuk-frontend/govuk/assets')
)
);
// Session uses service name to avoid clashes with other prototypes
const sessionName = Buffer.from(config.SERVICE_NAME, 'utf8').toString('hex');
const sessionOptions = {
secret: sessionName,
cookie: {
maxAge: 1000 * 60 * 60 * 4, // 4 hours
secure: isSecure
},
};
// if (env === 'development') {
// app.use(
// sessionInMemory(
// Object.assign(sessionOptions, {
// name: sessionName,
// resave: false,
// saveUninitialized: false,
// })
// )
// );
// } else {
app.use(
session(
Object.assign(sessionOptions, {
store: AzureTablesStoreFactory.create(),
resave: false,
saveUninitialized: false,
})
)
);
// }
// Manage session data. Assigns default values to data
app.use(sessionData);
// Logs req.session data
if (env === 'development') edt(app, { panels: ['session'] });
// Load API routes
app.use('/', routes());
app.get('/', function(req, res){
res.redirect(process.env.ROOT_URL);
})
// Disables caching when user clicks back button on confirmation page
app.use('/report/check-your-answers', function (req, res, next) {
res.set(
'Cache-Control',
'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
);
next();
});
app.get(/^([^.]+)$/, function (req, res, next) {
if (config.SERVICE_UNAVAILABLE === true) {
console.log('Service Unavailable.');
res.status('503');
res.sendFile(path.join(__dirname, '/app/static/service-unavailable.html'));
} else {
matchRoutes(req, res, next);
}
});
// Redirect all POSTs to GETs
app.post(/^\/([^.]+)$/, function (req, res) {
res.redirect('/' + req.params[0]);
});
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error(`Page not found: ${req.path}`);
err.status = 404;
next(err);
});
// Display error
app.use(function (err, req, res, next) {
res.status(err.status || 500);
if (err.message.indexOf('not found') > 0) {
res.status(404).render('404');
}
});
app.listen(PORT, () => {
console.log(`App listening on ${PORT} - url: http://localhost:${PORT}`);
console.log('Press Ctrl+C to quit.');
});