generated from Adedoyin-Emmanuel/nodejs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
86 lines (76 loc) · 2.29 KB
/
index.ts
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
import dotenv from "dotenv";
dotenv.config();
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import cors from "cors";
import express from "express";
import helmet from "helmet";
import swaggerJSDoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
import mongoSanitize from "express-mongo-sanitize";
import "express-async-errors";
import morgan from "morgan";
import { useErrorHandler, useNotFound, useRateLimiter } from "./middlewares/";
import { swaggerOptions } from "./docs/swagger.docs";
import { connectToDb } from "./utils";
import http from "http";
import { initSocket } from "./sockets/socket.server";
import {
helloRouter,
authRouter,
buyerRouter,
merchantRouter,
productRouter,
collectionRouter,
transactionRouter,
} from "./routes";
import { logger, redisClient } from "./utils";
const PORT = process.env.PORT || 2800;
const app = express();
const server = http.createServer(app);
initSocket(server);
const corsOptions = {
origin: (origin: any, callback: any) => {
// Check if the origin matches any of the patterns
if (
!origin ||
allowedOriginPatterns.some((pattern) => pattern.test(origin))
) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
};
//middlewares
const allowedOriginPatterns = [
/http:\/\/localhost:3000$/,
/^https:\/\/agronomix\.vercel\.app/,
];
app.use(cors(corsOptions));
app.use(cookieParser());
app.use(bodyParser.json({ limit: "100mb" }));
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(useRateLimiter);
app.use(helmet());
app.use(mongoSanitize());
//endpoints
app.use("/api", helloRouter);
app.use("/api/auth", authRouter);
app.use("/api/buyer", buyerRouter);
app.use("/api/buyer/collection", collectionRouter);
app.use("/api/merchant", merchantRouter);
app.use("/api/product", productRouter);
app.use("/api/transaction", transactionRouter);
const swaggerSpec = swaggerJSDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use(useNotFound);
app.use(useErrorHandler);
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
logger.info({ message: `...app listening on port ${PORT}` });
connectToDb();
});
export default server;