-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
101 lines (81 loc) · 2.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
// Core dependencies
const path = require('path');
// NPM dependencies
const express = require('express');
const nunjucks = require('nunjucks');
const markdown = require('nunjucks-markdown');
const marked = require('marked');
const cookieParser = require('cookie-parser');
const NunjucksCodeHighlight = require('nunjucks-highlight.js');
const hljs = require('highlight.js');
const fileHelper = require('./app/utils/file-helper');
const highlight = new NunjucksCodeHighlight(nunjucks, hljs);
// Routing
const routes = require('./app/routes/index');
const autoRoutes = require('./app/routes/auto');
// Middleware
const { errorHandler } = require('./app/middleware/errorHandler');
const { urlPartials } = require('./app/middleware/urlPartials');
// Port
const port = process.env.PORT || 3000;
// Application
const app = express();
// Setup application
const appViews = [
path.join(__dirname, '/node_modules/@dwp/dwp-frontend/'),
path.join(__dirname, '/node_modules/@dwp/dwp-frontend/components'),
path.join(__dirname, '/node_modules/govuk-frontend/govuk/'),
path.join(__dirname, '/node_modules/govuk-frontend/govuk/components'),
path.join(__dirname, 'app/views'),
path.join(__dirname, 'app/views/components'),
path.join(__dirname, 'app/views/patterns'),
path.join(__dirname, 'app/views/_layouts'),
path.join(__dirname, 'app/views/_partials'),
];
// Configurations
const nunjucksEnvironment = nunjucks.configure(appViews, {
autoescape: true,
express: app,
noCache: true,
watch: true,
});
nunjucksEnvironment.addGlobal('getMacroParams', fileHelper.getMacroParams);
nunjucksEnvironment.addGlobal('getNunjucksCode', fileHelper.getNunjucksCode);
nunjucksEnvironment.addGlobal('getHtmlCode', fileHelper.getHtmlCode);
nunjucksEnvironment.addGlobal('getCSSCode', fileHelper.getCSSCode);
nunjucksEnvironment.addExtension('NunjucksCodeHighlight', highlight);
// Set view engine
app.set('view engine', 'html');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// Middleware to serve static assets
app.use('/public', express.static(path.join(__dirname, '/public')));
app.use('/assets', express.static(path.join(__dirname, 'node_modules', 'govuk-frontend', 'govuk', 'assets')));
app.use(urlPartials);
// Use routes
app.use(routes);
app.use(autoRoutes);
app.use(errorHandler);
const renderer = new marked.Renderer();
marked.setOptions({
renderer,
gfm: true,
tables: true,
breaks: true,
pendantic: true,
sanitize: false,
smartLists: true,
smartypants: true,
});
// Markdown register
markdown.register(nunjucksEnvironment, marked);
// Start app
app.listen(port, (err) => {
if (err) {
throw err;
} else {
console.log('Listening on port 3000 url: http://localhost:3000');
}
});
module.exports = app;