-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (105 loc) · 2.89 KB
/
server.js
File metadata and controls
122 lines (105 loc) · 2.89 KB
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
122
import { PORT } from "./config/index.js";
import {} from "express-async-errors";
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import connectDB from "./db/connectDB.js";
import initRoutes from "./routes/index.js";
import path from "path";
import morgan from "morgan";
import cookieParser from "cookie-parser";
import { connectToMongoDB } from "./db/mongoClient.js";
import ErrorHandler from "./middleware/errorHandler.js";
import helmet from "helmet";
import rfs from "rotating-file-stream";
import http from "http";
import { Server } from "socket.io";
import swaggerJSDoc from "swagger-jsdoc";
import swaggerUI from "swagger-ui-express";
const app = express();
const corsOptions = {
origin: ["http://localhost:5173", "https://hungrezy-react.vercel.app"],
credentials: true,
};
app.use(cors(corsOptions));
const httpServer = http.createServer(app);
const io = new Server(httpServer, {
cors: corsOptions,
});
io.on("connection", (socket) => {
console.log("Client connected");
// Handle "new-announcement" event
socket.on("new-announcement", (data) => {
// Broadcast the announcement to all clients
io.emit("announcement", data);
});
socket.on("disconnect", () => {
console.log("Client disconnected");
});
});
const accessLogStream = rfs.createStream("access.log", {
interval: "1d", // rotate daily
path: "./log",
});
app.use(morgan("combined", { stream: accessLogStream }));
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan("dev"));
app.use(cookieParser());
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Set up static files middleware
app.use(express.static(path.join(path.resolve(), "public")));
app.get("/", (req, res) => {
res.json({ message: "Hello World..." });
});
app.use("/api", initRoutes());
// ERROR HANDLER MIDDLEWARE (Last middleware to use)
app.use(ErrorHandler);
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Hungrezy API",
version: "1.0.0",
description: "Hungrezy API Documentation",
},
servers: [
{
url: "https://hungrezy-backend-f2ku.onrender.com",
},
],
components: {
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
},
},
security: [
{
bearerAuth: [],
},
],
},
apis: ["./routes/*.js", "./server.js"],
};
const swaggerspec = swaggerJSDoc(options);
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerspec));
const port = PORT || 3000;
const start = async () => {
try {
await connectDB(process.env.MONGO_URI);
await connectToMongoDB();
httpServer.listen(port, () =>
console.log(`Server is listening on port ${port}...`)
);
} catch (error) {
console.log(error);
}
};
start();