forked from vikingeducation/project_mimirs_market
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (111 loc) · 3.3 KB
/
app.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var express = require("express");
var app = express();
// ----------------------------------------
// dotenv
// ----------------------------------------
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
// ----------------------------------------
// Body Parser
// ----------------------------------------
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
// ----------------------------------------
// Method Override
// ----------------------------------------
app.use((req, res, next) => {
var method;
if (req.query._method) {
method = req.query._method;
delete req.query._method;
} else if (typeof req.body === "object" && req.body._method) {
method = req.body._method;
delete req.body._method;
}
if (method) {
method = method.toUpperCase();
req.method = method;
}
next();
});
// ----------------------------------------
// Public
// ----------------------------------------
app.use(express.static(`${__dirname}/public`));
// ----------------------------------------
// Logging
// ----------------------------------------
var morgan = require("morgan");
app.use(morgan("tiny"));
app.use((req, res, next) => {
["query", "params", "body"].forEach(key => {
if (req[key]) {
var capKey = key[0].toUpperCase() + key.substr(1);
var value = JSON.stringify(req[key], null, 2);
console.log(`${capKey}: ${value}`);
}
});
next();
});
// ----------------------------------------
// Sessions/Cookies
// ----------------------------------------
var cookieSession = require("cookie-session");
app.use(
cookieSession({
name: "session",
keys: ["asdf1234567890qwer"]
})
);
// ----------------------------------------
// Flash Messages
// ----------------------------------------
var flash = require("express-flash-messages");
app.use(flash());
// ----------------------------------------
// Mongoose
// ----------------------------------------
var mongoose = require("mongoose");
app.use((req, res, next) => {
if (mongoose.connection.readyState) {
next();
} else {
require("./mongo")(req).then(() => next());
}
});
// ----------------------------------------
// Routes
// ----------------------------------------
var indexRoute = require("./routes/index");
app.use("/", indexRoute);
var cartRoute = require("./routes/cart");
app.use("/cart", cartRoute);
var adminRoute = require("./routes/admin");
app.use("/admin", adminRoute);
var analyticsRoute = require("./routes/analytics");
app.use("/analytics", analyticsRoute);
// ----------------------------------------
// Template Engine
// ----------------------------------------
var expressHandlebars = require("express-handlebars");
var helpers = require("./helpers");
var hbs = expressHandlebars.create({
partialsDir: "views/",
defaultLayout: "main",
helpers: helpers.registered
});
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
// ----------------------------------------
// Server
// ----------------------------------------
var port = process.env.PORT || process.argv[2] || 3000;
var host = "localhost";
var args;
process.env.NODE_ENV === "production" ? (args = [port]) : (args = [port, host]);
args.push(() => {
console.log(`Listening: http://${host}:${port}`);
});
app.listen.apply(app, args);
module.exports = app;