-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (66 loc) · 2.12 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import "dotenv/config";
import express from "express";
import "express-async-errors";
import mongoose from "mongoose";
import helmet from "helmet";
import cors from "cors";
import rateLimit from "express-rate-limit";
import cookieParser from "cookie-parser";
import commentRouter from "./routes/comment.js";
import postRouter from "./routes/post.js";
import tagRouter from "./routes/tag.js";
import authRouter from "./routes/user.js";
import followerRouter from "./routes/follower.js";
import notificationRouter from "./routes/notification.js";
// error handler
import errorHandlerMiddleware from "./middleware/error-handler.js";
import notFoundMiddleware from "./middleware/not-found.js";
const app = express();
const port = process.env.PORT || 5000;
app.set("trust proxy", 1);
const whitelist = ["http://localhost:3000", "https://penpages.netlify.app"];
const corsOptions = {
origin: (origin, callback) => {
if (!origin || whitelist.includes(origin)) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
optionsSuccessStatus: 200,
methods: ["GET", "POST", "DELETE", "PUT", "PATCH"],
credentials: true,
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(helmet());
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(
rateLimit({
windowMs: 15 * 60 * 1000, // 15 mins
max: 2000, // Adjust the limit if needed
})
);
app.get("/", (req, res) => {
res.send(
`Penpages API. <p>Checkout the <a href="https://documenter.getpostman.com/view/31014226/2sA2xnxpqp">PenPages API Documentation</a></p>`
);
});
app.use("/api/v1/user", authRouter);
app.use("/api/v1/post", postRouter);
app.use("/api/v1/tag", tagRouter);
app.use("/api/v1/comment", commentRouter);
app.use("/api/v1/follower", followerRouter);
app.use("/api/v1/notification", notificationRouter);
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const start = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
app.listen(port, () => console.log(`server is running on port ${port}`));
} catch (error) {
console.log(error);
}
};
start();