-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·112 lines (90 loc) · 3.01 KB
/
app.js
File metadata and controls
executable file
·112 lines (90 loc) · 3.01 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
// Core dependencies
const path = require('path')
// External dependencies
const bodyParser = require('body-parser')
const express = require('express');
const nunjucks = require('nunjucks');
// Local dependencies
const authentication = require('./middleware/authentication');
const automaticRouting = require('./middleware/auto-routing');
const config = require('./app/config');
const locals = require('./app/locals');
const routes = require('./app/routes');
const documentationRoutes = require('./docs/documentation_routes');
// Set configuration variables
const port = process.env.PORT || config.port;
const useDocumentation = process.env.SHOW_DOCS || config.useDocumentation;
const onlyDocumentation = process.env.DOCS_ONLY;
// Initialise applications
const app = express();
const documentationApp = express();
// Check if the app is documentation only
if(onlyDocumentation !== 'true') {
// Require authentication if not
app.use(authentication);
}
// Local variables
app.use(locals(config));
// View engine
app.set('view engine', 'html');
documentationApp.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')));
// Nunjucks configuration for application
var appViews = [
path.join(__dirname, 'app/views/'),
path.join(__dirname, 'node_modules/nhsuk-frontend/packages/components')
]
nunjucks.configure(appViews, {
autoescape: true,
express: app
});
// Support for parsing data in POSTs
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
// Check if the app is documentation only
if(onlyDocumentation == 'true') {
app.get('/', function(req, res) {
// Redirect to the documentation pages if it is
res.redirect('/docs');
});
} else {
// Else use custom application routes
app.use('/', routes);
}
// Automatically route pages
app.get(/^([^.]+)$/, function (req, res, next) {
automaticRouting.matchRoutes(req, res, next)
})
// Check if the app is using documentation
if (useDocumentation || onlyDocumentation == 'true') {
// Documentation routes
app.use('/docs', documentationApp);
// Nunjucks configuration for documentation
var docViews = [
path.join(__dirname, 'docs/views/'),
path.join(__dirname, 'node_modules/nhsuk-frontend/packages/components')
]
nunjucks.configure(docViews, {
autoescape: true,
express: documentationApp
});
// Support for parsing data in POSTs
documentationApp.use(bodyParser.json());
documentationApp.use(bodyParser.urlencoded({
extended: true
}))
// Custom documentation routes
documentationApp.use('/', documentationRoutes);
// Automatically route documentation pages
documentationApp.get(/^([^.]+)$/, function (req, res, next) {
automaticRouting.matchRoutes(req, res, next)
})
}
// Run the application
app.listen(port);
module.exports = app;