-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·69 lines (55 loc) · 1.95 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
"use strict";
var express = require("express");
var favicon = require("serve-favicon");
var bodyParser = require("body-parser");
var session = require("express-session");
var consolidate = require("consolidate"); // Templating library adapter for Express
var swig = require("swig");
var MongoClient = require("mongodb").MongoClient; // Driver for connecting to MongoDB
var http = require("http");
var marked = require("marked");
var app = express(); // Web framework to handle routing requests
var routes = require("./app/routes");
var config = require("./config/config"); // Application config properties
MongoClient.connect(config.db, function(err, db) {
if (err) {
console.log("Error: DB: connect");
console.log(err);
process.exit(1);
}
console.log("Connected to the database: " + config.db);
app.use(favicon(__dirname + "/app/assets/favicon.ico"));
// Express middleware to populate "req.body" so we can access POST variables
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
// Mandatory in Express v4
extended: false
}));
// Enable session management using express middleware
app.use(session({
secret: config.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"));
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(config.port, function() {
console.log("Express http server listening on port " + config.port);
});
});