-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
53 lines (42 loc) · 1.37 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
import express from "express";
// import morgan from "morgan";
import helmet from "helmet";
import rateLimit from "express-rate-limit";
import cors from "cors";
import ErrorResponse from "./utils/errorResponse";
import { resource404Error } from "./utils/errorObject";
import errorHandler from "./middlewares/errorHandler";
// import routes
import categories from "./routers/categories";
import products from "./routers/products";
import customers from "./routers/customers";
import admins from "./routers/admins";
import auth from "./routers/auth";
import orders from "./routers/orders";
const app = express();
// Enable CORS
app.use(cors());
// Set HTTP Hseaders
app.use(helmet());
// Set Rate Limit
const limiter = rateLimit({
windowMs: 7 * 60 * 1000, // 7 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
app.use(limiter);
// process.env.NODE_ENV === "development" && app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Routes
app.use("/api/v1/categories", categories);
app.use("/api/v1/products", products);
app.use("/api/v1/orders", orders);
app.use("/api/v1/customers", customers);
app.use("/api/v1/auth", auth);
app.use("/api/v1/admins", admins);
// 404 error if route not found
app.all("*", (req, res, next) =>
next(new ErrorResponse(resource404Error("route"), 404))
);
app.use(errorHandler);
export default app;