-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (97 loc) · 3.1 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
const path = require("path");
const express = require("express");
const exphbs = require("express-handlebars");
const session = require("express-session");
const mongoose = require("mongoose");
const moment = require("moment");
const morgan = require("morgan");
const methodOverride = require("method-override");
// var bodyParser = require("body-parser");
const dotenv = require("dotenv");
const passport = require("passport");
const MongoStore = require("connect-mongo")(session);
const connectDB = require("./config/database"); // config for database connection
// loading configuration file from path
// "./config/config.env" to access environment
// variables such as PORT, NODE_ENV, etc. that
// can be used globally for various reasons
// including but not limited to segregating between
// production and development environments
dotenv.config({ path: "./config/config.env" });
const PORT = process.env.PORT || 5000;
const NODE_ENV = process.env.NODE_ENV;
// adding oauth strategies
require("./config/google_passport")(passport);
require("./config/github_passport")(passport);
// connection to the database using config
connectDB();
// initialising an express app
app = express();
// adding body parser middleware
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// adding morgan middleware for logging
if (NODE_ENV === "development") {
app.use(morgan("dev"));
console.log("Using morgan middleware (dev) for logging");
}
// helpers for handlebars
const {
formatDate,
stripTags,
truncate,
editIcon,
select,
} = require("./helpers/hbs");
// initialising express-handlebars for templating
app.engine(
".hbs",
exphbs({
helpers: { formatDate, stripTags, truncate, editIcon, select },
defaultLayout: "main.hbs",
extname: ".hbs",
})
);
app.set("view engine", ".hbs");
// adding middleware for session
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);
// adding middleware for passport
app.use(passport.initialize());
app.use(passport.session());
// adding middleware to make "user" global
app.use((req, res, next) => {
res.locals.user = req.user || null;
return next();
});
// adding middleware for method-override
// app.use(bodyParser.urlencoded());
app.use(
methodOverride(function (req, res) {
if (req.body && typeof req.body === "object" && "_method" in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method;
delete req.body._method;
return method;
}
})
);
// adding public directory for assets
app.use(express.static(path.join(__dirname, "public")));
// adding routes for "/..." endpoints
app.use("/", require("./routes/index.js"));
// adding routes for "/auth/..." endpoints
app.use("/auth", require("./routes/auth.js"));
// adding routes for "/stories/..." endpoints
app.use("/stories", require("./routes/stories.js"));
// initialising express app to listen to
// any incoming requests
app.listen(PORT, (req, res) => {
console.log(`Server running in ${NODE_ENV} at port: ${PORT}`);
});