forked from iampavangandhi/TradeByte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (99 loc) · 2.95 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
// Welcome to TradeByte
//jshint esversion: 8
// Includes
const path = require("path");
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const methodOverride = require("method-override");
const expressLayouts = require("express-ejs-layouts");
// Auth and DB Includes
const passport = require("passport");
const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo")(session);
const connectDB = require("./config/db");
const flash = require("connect-flash");
var cookieParser = require("cookie-parser");
// Load config
dotenv.config({ path: "./config/config.env" });
// Passport config
require("./config/passport")(passport);
require("./config/passportLocal")(passport);
// DB Connected
connectDB();
// App init
const app = express();
// Cache Fix
app.disable("etag");
// Body parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Method override
app.use(
methodOverride(function (req, res) {
if (req.body && typeof req.body === "object" && "_method" in req.body) {
let method = req.body._method;
delete req.body._method;
return method;
}
})
);
// Logging
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// EJS
app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
// Express layouts
app.set("layout", "layouts/login", "layouts/app");
app.use(expressLayouts);
// Sessions
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);
// cookie middleware
app.use(cookieParser());
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect Flash
app.use(flash());
// Set Global variables
app.use(function (req, res, next) {
res.locals.user = req.user || null;
res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");
next();
});
// Static folder
app.use(express.static(path.join(__dirname, "/public")));
// Routes
app.use("/", require("./routes/api/index"));
app.use("/auth", require("./routes/api/auth"));
app.use("/user", require("./routes/api/user"));
app.use("/portfolio", require("./routes/api/portfolio"));
app.use("/market", require("./routes/api/market"));
app.use("/view", require("./routes/api/view"));
app.use("/cart", require("./routes/api/cart"));
app.use("/done", require("./routes/api/done"));
app.use("/addBalance", require("./routes/api/addBalance"));
app.use("/transaction", require("./routes/api/transaction"));
app.use("/search", require("./routes/api/search"));
app.use("/edit", require("./routes/api/edit"));
// Port: Love You 3000
const PORT = process.env.PORT || 3000;
// Server Listening
app.listen(
PORT,
console.log(
`TradeByte is running in ${process.env.NODE_ENV} mode on port ${PORT}`
)
);