forked from henrylle/bia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (65 loc) · 1.98 KB
/
index.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
75
76
77
78
79
80
81
82
var express = require("express");
var logger = require("morgan");
var path = require("path");
var session = require("express-session");
var methodOverride = require("method-override");
var app = express();
// define a custom res.message() method
// which stores messages in the session
app.response.message = function (msg) {
// reference `req.session` via the `this.req` reference
var sess = this.req.session;
// simply add the msg to an array for later
sess.messages = sess.messages || [];
sess.messages.push(msg);
return this;
};
// log
app.use(logger("dev"));
// serve static files
app.use(express.static(path.join(__dirname, "app", "public")));
app.use(express.static(path.join(__dirname, "client", "build")));
// session support
app.use(
session({
resave: false, // don't save session if unmodified
saveUninitialized: false, // don't create session until something stored
secret: "some secret here",
})
);
// parse request bodies (req.body)
app.use(express.urlencoded({ extended: true }));
// allow overriding methods in query (?_method=put)
app.use(methodOverride("_method"));
// expose the "messages" local variable when views are rendered
app.use(function (req, res, next) {
var msgs = req.session.messages || [];
// expose "messages" local variable
res.locals.messages = msgs;
// expose "hasMessages"
res.locals.hasMessages = !!msgs.length;
/* This is equivalent:
res.locals({
messages: msgs,
hasMessages: !! msgs.length
});
*/
next();
// empty or "flush" the messages so they
// don't build up
req.session.messages = [];
});
// load controllers
require("./lib/boot")(app, { verbose: false });
app.use(function (err, req, res, next) {
// log it
console.error(err.stack);
// error page
res.status(500).render("5xx");
});
// assume 404 since no middleware responded
app.use(function (req, res, next) {
res.status(404).render("404", { url: req.originalUrl });
});
app.listen(3000);
console.log("Express started on port 3000");