-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (61 loc) · 1.59 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
// Add items to `process.env`
require('dotenv').config();
const express = require('express');
const morgan = require('morgan');
const compression = require('compression');
const bodyParser = require('body-parser');
const nunjucks = require('nunjucks');
const path = require('path');
const config = require('./config.js');
const database = require('./database.js');
function startServer() {
const app = express();
const router = require('./router.js');
// Setup the server
if (!config.production) {
app.use(morgan('dev'));
} else {
app.use(compression());
}
app.disable('x-powered-by');
app.use(
bodyParser.urlencoded({
extended: true
})
);
// Set "./views" as the views folder
nunjucks.configure('views', {
express: app,
noCache: !config.production
});
// Set default template extension to .html
app.set('view engine', 'html');
app.locals.css = ['/css/main.css'];
app.locals.js = [];
app.locals.domain = config.domain;
// Public files
app.use(express.static(path.join(config.root, 'public')));
app.use(router);
// Catch-all/404
app.use((req, res, next) => {
res.status(404);
res.render('error', {
title: 'Not Found',
error: {
name: '404 - Page not found',
description: 'The page at <b>' + req.path + '</b> was not found. Go <a href="/">home</a>.'
}
});
});
// Open the port for business
app.listen(config.port, () => {
console.log(`=> Running on ${config.domain}`);
});
}
database.init(err => {
if (err) {
console.error('Database error:', err);
return;
}
startServer();
});