-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
executable file
·56 lines (45 loc) · 1.47 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
#!/usr/bin/env node
const routes = require('./lib/routes');
const PORT = parseInt(process.env.PORT, 10) || 8900;
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
const Handlebars = require('handlebars');
const { engine } = require('express-handlebars');
app.engine('handlebars', engine({
helpers: {
addCSS: (file) => { //Needs expanded to do combo files in the future
const html = `<link rel="stylesheet" href="/static${file}">`;
return new Handlebars.SafeString(html);
}
}
}));
app.set('view engine', 'handlebars');
app.set('views', './views');
app.use('/static', express.static('static'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Hacks to make express compatible from Hapi definitions
app.use((req, res, next) => {
res.view = (template, locals, layout) => {
res.locals = locals;
res.render(template, layout);
};
res.file = (filename) => {
const path = require('path');
res.download(filename, path.basename(filename));
};
next();
});
// Hacks to make express compatible from Hapi definitions
routes.forEach(r => {
const method = r.method.toLowerCase();
const path = r.path.replace('}', '').replace('{', ':');
app[method](path, r.handler);
});
// Send all 404's home
app.use((req, res) => {
res.redirect('/');
});
module.exports.server = app.listen(PORT);
module.exports.app = app;