-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (46 loc) · 1.37 KB
/
server.js
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 from "express";
import "colors";
import dotenv from "dotenv";
import morgan from "morgan";
import connectDB from "./config/db.js";
import authRoutes from "./routes/authRoutes.js";
import categoryRoutes from "./routes/categoryRoutes.js";
import productRoutes from "./routes/productRoutes.js";
import cors from "cors";
//configuring dotenv
dotenv.config();
//connecting to database
connectDB();
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.json()); // Enable JSON parsing in req and response
app.use(morgan("dev"));
app.use(
cors({
origin: [
"http://127.0.0.1:5173",
"https://nex-ecom.vercel.app",
],
methods: "GET, POST, PUT, DELETE",
allowedHeaders: "Content-Type, Authorization",
credentials: true,
})
);
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'https://nex-ecom.vercel.app');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
//routes
app.use("/api/auth", authRoutes);
app.use("/api/category", categoryRoutes);
app.use("/api/product", productRoutes);
// listening to port
app.listen(PORT, () => {
console.log(`server is running on port ${PORT}`.bgCyan.white);
});
//api's
app.get("/", (req, res) => {
res.send("<h1>hello world</h1>");
});