-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (51 loc) · 1.46 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
var express = require("express");
var path = require("path");
var logger = require("morgan");
var compression = require("compression");
var methodOverride = require("method-override");
var session = require("express-session");
var flash = require("express-flash");
var bodyParser = require("body-parser");
var expressValidator = require("express-validator");
var dotenv = require("dotenv");
var nunjucks = require("nunjucks");
// Load environment variables from .env file
dotenv.load();
// Routes
var router = require("./routes");
var app = express();
// view engine setup
nunjucks.configure("views", {
autoescape: true,
watch: process.env.NODE_ENV === "development",
express: app
});
app.set("view engine", "html");
app.set("port", process.env.PORT || 3000);
app.use(compression());
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(methodOverride("_method"));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
})
);
app.use(flash());
app.use(express.static(path.join(__dirname, "public")));
app.use(router());
// Production error handler
if (app.get("env") === "production") {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get("port"), function() {
console.log("Express server listening on port " + app.get("port"));
});
module.exports = app;