-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (72 loc) · 2.64 KB
/
server.js
File metadata and controls
84 lines (72 loc) · 2.64 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
// 1. Import Exprerss
import express from "express";
import swagger from "swagger-ui-express";
import dotenv from "dotenv";
import productRouter from "./src/features/product/product.routes.js";
import userRouter from "./src/features/user/user.routes.js";
import jwtAuth from "./src/middlewares/jwt.middleware.js";
import cartRouter from "./src/features/cartItems/cartItems.routes.js";
import apiDocs from "./swagger.json" assert { type: "json" };
import loggerMiddleware from "./src/middlewares/logger.middleware.js";
import { ApplicationError } from "./src/error-handler/applicationError.js";
import { connectToMongoDB } from "./src/config/mongodb.js";
import orderRouter from "./src/features/order/order.routes.js";
import { connectUsingMongoose } from "./src/config/mongooseConfig.js";
import mongoose from "mongoose";
import likeRouter from "./src/features/like/like.routes.js";
// 2. Create Server
const server = express();
// load all the environment variables in application
dotenv.config();
// CORS policy configuration
server.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "http://localhost:5500");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "*");
// return ok for preflight request.
if (req.method == "OPTIONS") {
return res.sendStatus(200);
}
next();
});
server.use(express.json());
// Bearer <token>
// for all requests related to product, redirect to product routes.
// localhost:3200/api/products
server.use("/api-docs", swagger.serve, swagger.setup(apiDocs));
server.use(loggerMiddleware);
server.use("/api/orders", jwtAuth, orderRouter);
server.use("/api/products", jwtAuth, productRouter);
server.use("/api/cartItems", loggerMiddleware, jwtAuth, cartRouter);
server.use("/api/users", userRouter);
server.use("/api/likes", jwtAuth, likeRouter);
// 3. Default request handler
server.get("/", (req, res) => {
res.send("Welcome to Ecommerce APIs");
});
// Error handler middleware
server.use((err, req, res, next) => {
console.log(err);
if (err instanceof mongoose.Error.ValidationError) {
return res.status(400).send(err.message);
}
if (err instanceof ApplicationError) {
return res.status(err.code).send(err.message);
}
// server errors.
res.status(500).send("Something went wrong, please try later");
});
// 4. Middleware to handle 404 requests.
server.use((req, res) => {
res
.status(404)
.send(
"API not found. Please check our documentation for more information at localhost:3200/api-docs",
);
});
// 5. Specify port.
server.listen(3200, () => {
console.log("Server is running at 3200");
// connectToMongoDB();
connectUsingMongoose();
});