-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (70 loc) · 2.42 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
var path = require('path'),
express = require('express'),
swig = require('swig'),
swig_extras = require('swig-extras'),
session = require('express-session'),
routes = require(__dirname + '/app/routes.js'),
db = require(__dirname + '/db/load.js'),
app = express(),
port = (process.env.PORT || 3000),
// Grab environment variables specified in Procfile or as Heroku config vars
username = process.env.USERNAME,
password = process.env.PASSWORD,
env = process.env.NODE_ENV || 'development';
// Application settings
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/app/views');
// Template engine settings
// Swig will cache templates for you, but you can disable
// that and use Express's caching instead, if you like:
app.set('view cache', false);
// To disable Swig's cache, do the following:
swig.setDefaults({ cache: false });
// NOTE: You should always cache templates in a production environment.
// Don't leave both of these to `false` in production!
// Set base directory for Swig templates and includes
swig.setDefaults({ loader: swig.loaders.fs(__dirname + '/app/views' )});
// Set up markdown
swig_extras.useTag(swig, 'markdown');
// Middleware to serve static assets
app.use('/public', express.static(__dirname + '/public'));
app.use('/public', express.static(__dirname + '/app/assets_govuk_legacy'));
app.use('/public', express.static(__dirname + '/nhsalpha_modules/nhsalpha_frontend_toolkit'));
app.use(express.favicon(path.join(__dirname, 'app', 'assets_govuk_legacy', 'images','favicon.ico')));
// send assetPath to all views
app.use(function (req, res, next) {
res.locals({'assetPath': '/public/'});
next();
});
// set up sessions
app.use(session({
secret: 'this is actually public'
,resave: true,
saveUninitialized: true}));
// give views/layouts direct access to session data
app.use(function(req, res, next){
res.locals.session = req.session;
next();
});
// make everything in db/*.json available in app.locals
db.load(app);
// routes (found in app/routes.js)
routes.bind(app);
// auto render any view that exists
app.get(/^\/([^.]+)$/, function (req, res) {
var path = (req.params[0]);
res.render(path, function(err, html) {
if (err) {
console.log(err);
res.send(404);
} else {
res.end(html);
}
});
});
// start the app
app.listen(port);
console.log('');
console.log('Listening on port ' + port);
console.log('');