-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
60 lines (50 loc) · 2.23 KB
/
app.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
import express, { NextFunction, Request, Response } from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import { rateLimit } from 'express-rate-limit'
import ErrorMiddleware from "./middleware/error";
import userRouter from "./routes/user.route";
import courseRouter from "./routes/course.route";
import orderRouter from "./routes/order.route";
import notificationRouter from "./routes/notification.route";
import analyticsRouter from "./routes/analytics.route";
import layoutRouter from "./routes/layout.route";
// create our express server
export const app = express();
//use the cors middleware: Cross-origin resource sharing (CORS)
//is a mechanism that allows restricted resources on a web page
//to be accessed from another domain outside the domain from which
//the first resource was served.
app.use(cors({ origin: ['https://e-learning-client-nu.vercel.app'],
credentials: true }));
// body parser wih the limit for the JSON payload size. the maximum size 50 megabytes.
app.use(express.json({ limit: "50mb" }));
//cookieParser: Parse Cookie header and populate req.cookies with an object keyed by the cookie names.
app.use(cookieParser());
// routes
app.use('/api/v1', userRouter, courseRouter, orderRouter, notificationRouter, analyticsRouter, layoutRouter);
//testing our API
app.get("/testing", (req: Request, res: Response, next: NextFunction) => {
res.status(200).json({
success: true,
message: "API is working",
});
});
// unknown routes
app.all("*", (req: Request, res: Response, next: NextFunction) => {
const err = new Error(`route ${req.originalUrl} not found`) as any;
err.statusCode = 404;
next(err);
});
// handling errors
app.use(ErrorMiddleware);
// limit the reqs (to protced the serever from many reqs)
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
// store: ... , // Use an external store for consistency across multiple server instances.
})
// Apply the rate limiting middleware to all requests.
app.use(limiter);