-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·204 lines (168 loc) · 5.11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const express = require("express");
const app = express();
const _ = require("lodash");
const moment = require("moment");
// ----------------------------------------
// Passport
// ----------------------------------------
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
app.use(passport.initialize());
// ----------------------------------------
// App constiables
// ----------------------------------------
app.locals.appName = "MiniNanoWriMo";
app.locals.resetTime = moment(Date.now())
.add(1, "d")
.format("HH:mm");
// ----------------------------------------
// Express Sessions
// ----------------------------------------
const expressSession = require("express-session");
app.use(passport.session());
// ----------------------------------------
// Mongoose
// ----------------------------------------
const mongoose = require("mongoose");
// mongoose.connect("mongodb://localhost/hackathon_test_development");
app.use((req, res, next) => {
if (mongoose.connection.readyState) {
next();
} else {
require("./mongo")().then(() => next());
}
});
// ----------------------------------------
// ENV
// ----------------------------------------
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
// ----------------------------------------
// Body Parser
// ----------------------------------------
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
// ----------------------------------------
// Sessions/Cookies
// ----------------------------------------
const cookieParser = require("cookie-parser");
const cookieSession = require("cookie-session");
app.use(cookieParser());
app.use(
cookieSession({
name: "session",
keys: [process.env.SESSION_SECRET || "secret"]
})
);
app.use((req, res, next) => {
res.locals.session = req.session;
next();
});
// ----------------------------------------
// Flash Messages
// ----------------------------------------
const flash = require("express-flash-messages");
app.use(flash());
// ----------------------------------------
// Method Override
// ----------------------------------------
const methodOverride = require("method-override");
const getPostSupport = require("express-method-override-get-post-support");
app.use(
methodOverride(
getPostSupport.callback,
getPostSupport.options // { methods: ['POST', 'GET'] }
)
);
// ----------------------------------------
// Referrer
// ----------------------------------------
app.use((req, res, next) => {
req.session.backUrl = req.header("Referer") || "/";
next();
});
// ----------------------------------------
// Public
// ----------------------------------------
app.use(express.static(`${__dirname}/public`));
// ----------------------------------------
// Logging
// ----------------------------------------
const morgan = require("morgan");
const morganToolkit = require("morgan-toolkit")(morgan, {
req: ["cookies" /*, 'signedCookies' */]
});
app.use(morganToolkit());
// ----------------------------------------
// Local Strategy
// ----------------------------------------
const User = require("./models/user");
passport.use(
new LocalStrategy(function(email, password, done) {
User.findOne({ email }, function(err, user) {
if (err) return done(err);
if (!user || !user.validPassword(password)) {
return done(null, false, { message: "Invalid email/password" });
}
return done(null, user);
});
})
);
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// ----------------------------------------
// Routes
// ----------------------------------------
const indexRouter = require("./routers/index");
app.use("/", indexRouter);
const postsRouter = require("./routers/posts");
app.use("/posts", postsRouter);
// ----------------------------------------
// Template Engine
// ----------------------------------------
const expressHandlebars = require("express-handlebars");
const helpers = require("./helpers");
const hbs = expressHandlebars.create({
helpers: helpers,
partialsDir: "views/",
defaultLayout: "application"
});
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
// ----------------------------------------
// Server
// ----------------------------------------
const port = process.env.PORT || 3000;
const host = "localhost";
let args;
process.env.NODE_ENV === "production" ? (args = [port]) : (args = [port, host]);
args.push(() => {
console.log(`Listening: http://${host}:${port}\n`);
});
// if (require.main === module) {
// app.listen.apply(app, args);
// }
console.log("---------------------");
console.log("PORT => ", port);
console.log("---------------------");
app.listen(port);
// ----------------------------------------
// Error Handling
// ----------------------------------------
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
if (err.stack) {
err = err.stack;
}
res.status(500).render("errors/500", { error: err });
});
module.exports = app;