-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (61 loc) · 2.06 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
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
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import http, { createServer } from "http";
import morgan from "morgan";
import connectDB from "./config/db.js";
import { errorHandler, notFound } from "./middleware/errorMiddleware.js";
import userRoutes from "./routes/userRoutes.js";
import brandRoutes from "./routes/brandRoutes.js";
import categoryRoutes from "./routes/categoryRoutes.js";
import storeRoutes from "./routes/storeRoutes.js";
import attributeRoutes from "./routes/attributeRoutes.js";
import productRoutes from "./routes/productRoutes.js";
import orderRoutes from "./routes/orderRoutes.js";
import employeeRoutes from "./routes/employeeRoutes.js";
import { Server } from "socket.io";
dotenv.config();
connectDB();
const app = express();
app.use(cors());
app.use(morgan("dev"));
app.use(express.json());
const httpServer = createServer(app);
const io = new Server(httpServer, {
transports: ["polling"],
cors: {
cors: {
origin: "http://localhost:3000",
},
},
});
app.get("/", (req, res) => {
res.send(
" End points are: <br/> /api/v1/users<br/> /api/v1/brands<br/> /api/v1/categories <br/> /api/v1/stores /api/v1/attributes <br/> /api/v1/products<br/> /api/v1/orders <br/> /api/v1/employees"
);
});
// api end points
app.use("/api/v1/users", userRoutes);
app.use("/api/v1/brands", brandRoutes);
app.use("/api/v1/category", categoryRoutes);
app.use("/api/v1/stores", storeRoutes);
app.use("/api/v1/attributes", attributeRoutes);
app.use("/api/v1/products", productRoutes);
app.use("/api/v1/orders", orderRoutes);
app.use("/api/v1/employees", employeeRoutes);
app.use(notFound);
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
io.on("connection", (socket) => {
console.log("A user is connected");
socket.on("message", (message) => {
console.log(`message from ${socket.id} : ${message}`);
});
socket.on("disconnect", () => {
console.log(`socket ${socket.id} disconnected`);
});
});
httpServer.listen(PORT, () => {
console.log(`Server up and running on port ${PORT}`);
});
export { io };