-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (49 loc) · 1.54 KB
/
Copy pathserver.js
File metadata and controls
59 lines (49 loc) · 1.54 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
import dotenv from "dotenv";
import express from "express";
import authRoutes from "./routes/auth.js";
import connectDB from "./config/db.js";
import helmet from "helmet";
import cors from "cors";
import rateLimit from "express-rate-limit";
import homeRoutes from "./routes/home.js";
import productRoutes from "./routes/product.js";
import cartRoutes from "./routes/cart.js";
import orderRoutes from "./routes/order.js";
import wishlistRoutes from "./routes/wishlist.js";
import userRoutes from "./routes/user.js";
dotenv.config();
connectDB();
const app = express();
const port = process.env.PORT || 3000;
app.use(helmet());
app.use(cors());
app.use(express.json());
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // max requests per IP
message: "Too many requests, please try after some time",
});
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: "Too many login/signup attempts",
});
app.use(limiter);
app.get("/", (req, res) => {
res.send("🚀 API is running. Use /api routes to access endpoints. 😊");
});
app.use("/api/auth", authLimiter, authRoutes);
app.use("/api/user", userRoutes);
app.use("/api/home", homeRoutes);
app.use("/api/product", productRoutes);
app.use("/api/cart", cartRoutes);
app.use("/api/order", orderRoutes);
app.use("/api/wishlist", wishlistRoutes);
app.use((err, req, res, next) => {
res.status(500).json({
message: err.message || "Internal Server Error",
});
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});