-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (82 loc) · 2.35 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
require("dotenv").config();
const passport = require("./config/passport");
const home = require("./routes/home");
const admin = require("./routes/admin");
const orders = require("./routes/orders");
const pizzas = require("./routes/pizzas");
const users = require("./routes/users");
const cart = require("./routes/cart");
const EventEmitter = require("events");
const methodOverride = require("method-override");
const session = require("express-session");
const flash = require("express-flash");
const MongoStore = require("connect-mongo");
const mongoose = require("mongoose");
const expressLayouts = require("express-ejs-layouts");
const express = require("express");
const app = express();
mongoose
.connect(process.env.DB)
.then(() => {
console.log("Connected to the database...");
})
.catch((err) => {
console.log("Could not connect to database...", err);
});
// setting up the application
const emitter = new EventEmitter();
app.set("view engine", "ejs");
app.set("emitter", emitter);
// middlewares
app.use(expressLayouts);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(methodOverride("_method"));
app.use(
session({
secret: process.env.COOKIE_SECRET,
resave: false,
saveUninitialized: true,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
store: MongoStore.create({
mongoUrl: process.env.DB,
collectionName: "sessions",
}),
})
);
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
res.locals.session = req.session;
res.locals.user = req.user;
next();
});
// routes
app.use("/", home);
app.use("/pizzas", pizzas);
app.use("/cart", cart);
app.use("/users", users);
app.use("/orders", orders);
app.use("/admin", admin);
app.get("/me", (req, res) => {
res.send(req.user);
});
const port = process.env.PORT || 3000;
const server = app.listen(port, () =>
console.log(`Listening on port ${port}...`)
);
const io = require("socket.io")(server);
io.on("connection", (socket) => {
socket.on("join", (roomName) => {
socket.join(roomName);
console.log("Joined in the room...");
});
});
emitter.on("orderUpdated", (orderData) => {
io.to(`order_${orderData._id}`).emit("orderUpdated", orderData);
});
emitter.on("orderPlaced", (order) => {
io.to("adminRoom").emit("orderPlaced", order);
});