-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (58 loc) · 2.04 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
"use strict";
const express = require("express");
const favicon = require("serve-favicon");
const bodyParser = require("body-parser");
const session = require("express-session");
// const csrf = require('csurf');
const consolidate = require("consolidate"); // Templating library adapter for Express
const swig = require("swig");
// const helmet = require("helmet");
const MongoClient = require("mongodb").MongoClient; // Driver for connecting to MongoDB
const http = require("http");
const marked = require("marked");
//const nosniff = require('dont-sniff-mimetype');
const app = express(); // Web framework to handle routing requests
const routes = require("./app/routes");
const { port, db, cookieSecret } = require("./config/config"); // Application config properties
MongoClient.connect(db, (err, db) => {
if (err) {
console.log("Error: DB: connect");
console.log(err);
process.exit(1);
}
console.log(`Connected to the database: ${db}`);
app.use(favicon(__dirname + "/app/assets/favicon.ico"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
// Mandatory in Express v4
extended: false
}));
// Enable session management using express middleware
app.use(session({
secret: cookieSecret,
// Both mandatory in Express v4
saveUninitialized: true,
resave: true
}));
// Register templating engine
app.engine(".html", consolidate.swig);
app.set("view engine", "html");
app.set("views", `${__dirname}/app/views`);
app.use(express.static(`${__dirname}/app/assets`));
// Initializing marked library
marked.setOptions({
sanitize: true
});
app.locals.marked = marked;
// Application routes
routes(app, db);
// Template system setup
swig.setDefaults({
// Autoescape disabled
autoescape: false
});
// Insecure HTTP connection
http.createServer(app).listen(port, () => {
console.log(`Express http server listening on port ${port}`);
});
});