-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (35 loc) · 1.03 KB
/
server.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
import express from "express";
import mongoose from "mongoose";
import { foodsRoutes } from "./routes/foodsRoutes.js";
import { usersRoutes } from "./routes/usersRoutes.js";
import job from "./utils/cron.js";
import { Server } from "socket.io";
const app = express();
app.use(express.json({ limit: "200mb" }));
app.use("/api/foods", foodsRoutes);
app.use("/api/users", usersRoutes);
// job.start();
const server = app.listen(4000, () => console.log("Listening at 4000"));
const io = new Server(server);
io.on("connection", (socket) => {
console.log("A user connected");
socket.on("turnOn", () => {
console.log("Turned on");
io.emit("lightOn");
});
socket.on("turnOff", () => {
console.log("Turned off");
io.emit("lightOff");
});
socket.on("disconnect", () => {
console.log("A user disconnected");
});
});
mongoose
.connect("mongodb+srv://admin:[email protected]/", {
dbName: "foodapp",
})
.then(() => {
console.log("Successfully connect to DB");
})
.catch((err) => console.log(err));