-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
57 lines (48 loc) · 1.76 KB
/
server.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
// creating a server
import express from "express";
import dotenv from "dotenv";
import { config } from "./config/config";
import connectDb from "./config/db";
import userRegistrationRouter from "./routes/userRegistrationRouter";
import globalErrorHandler from "./middlewares/globalErrorHandler";
import cookieParser from "cookie-parser";
import productRouter from "./routes/productRouter";
import morgan from "morgan";
import blogRouter from "./routes/blogRouter";
import categoryRouter from "./routes/categoryRoutes";
import { authentication, isAdmin } from "./middlewares/authenticate";
import brandRouter from "./routes/brandRouter";
import blogCategoryRouter from "./routes/blogCategoryRouter";
import couponRouter from "./routes/couponRouter";
const app = express();
const port = config.port;
app.use(express.json()); // to parse json data
//cookie parser
app.use(cookieParser()); // to parse cookies. what is cookie parser? It is a middleware that parses cookies attached to the client request object. It makes cookies available in the request object as req.cookies.
app.use(morgan("dev"));
// starting
const startServer = async () => {
await connectDb();
const port = config.port || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
};
// server site
app.get("/", (req, res) => {
res.send("Hello World");
});
//user registration
app.use("/api/user", userRegistrationRouter);
// all product routes
app.use("/api/product", productRouter);
//all blog routes
app.use("/api/blog", blogRouter);
//all category routes
app.use("/api/category", categoryRouter);
app.use("/api/brand", brandRouter);
app.use("/api/blogcategory", blogCategoryRouter);
app.use("/api/coupon", couponRouter);
//handling error
app.use(globalErrorHandler);
startServer();