-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (53 loc) · 1.66 KB
/
index.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
import express from "express";
import "dotenv/config";
import bodyParser from "body-parser";
import { userRouter } from "./routes/userRouter.js";
import errorHandler from "./middlewares/errorHandler.js";
import { blogRouter } from "./routes/blogRoutes.js";
import helmet from "helmet";
import cors from "cors";
import { limiter } from "./config/ratelimiter.js";
import compression from "compression";
import { prisma } from "./db/dbConfig.js";
import { redisCache } from "./config/redis.config.js";
// Initialize application
const app = express();
// Define port
const PORT = process.env.PORT || 8000;
// Add middlewares
app.use(
compression({
level: 6,
threshold: 0,
filter: (req, res) => {
if (req.headers["x-no-compression"]) {
return false;
}
return compression.filter(req, res);
},
})
);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(errorHandler);
app.use(helmet()); // For additional security
app.use(cors()); // CORS can be further modified - default for development
app.use(limiter); // Applied rate limiter
// Ping Test
app.get("/", (req, res) => {
res.status(200).json({ message: "Server is up and running" });
});
// This endpoint will reset the database and cache
app.get("/reset", async (req, res) => {
redisCache.client.flushdb(() => {});
await prisma.user.deleteMany({});
await prisma.blog.deleteMany({});
res.status(200).json({ message: "Database and cache has been cleared." });
});
// Add Routers
app.use("/api/user", userRouter);
app.use("/api/blog", blogRouter);
// Listen app to defined PORT
app.listen(PORT, () => {
console.log("Listening to: localhost:8000");
});