-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
79 lines (62 loc) · 1.87 KB
/
index.ts
File metadata and controls
79 lines (62 loc) · 1.87 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
// TODO: Configure Serverless
import express, { Express, Request, Response } from "express";
import mongoose, { ConnectOptions } from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import helmet from "helmet";
import morgan from "morgan";
import { image, post, auth, general, mail } from "./routes/index.js";
import { verifyToken } from "./middlewares/auth.js";
import RateLimit from "express-rate-limit";
import mongoSanitize from "express-mongo-sanitize";
/* Config */
dotenv.config();
const CONNECTION_URL: string = process.env.CONNECTION_URL || "";
const PORT = process.env.PORT || 5000;
const app: Express = express();
app.use(express.json({ limit: "30mb" }));
app.use(express.urlencoded({ limit: "30mb", extended: true }));
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
app.use(morgan("common"));
app.use(
cors({
origin: "*",
})
);
/* Set up rate limiter */
var limiter = RateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 15,
});
app.use(limiter);
/* Sanitize data */
app.use(mongoSanitize());
app.use("/media", express.static(process.cwd() + "/public/uploads/"));
/* Routes */
app.use("/auth", auth);
app.use("/posts", verifyToken, post);
app.use("/media/imgs", verifyToken, image);
// TODO: Add Client Token
app.use("/general", general);
app.use("/contact", mail);
app.get("/", (_req: Request, res: Response) => {
return res.send("ozxk blog api 🚀");
});
app.get("/ping", (_req: Request, res: Response) => {
return res.send("pong 🏓");
});
/* MongoDB Connection */
mongoose.set("strictQuery", false);
mongoose
.connect(CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as ConnectOptions)
.then(() =>
app.listen(PORT, () =>
console.log(`Server Running on Port: http://localhost:${PORT}`)
)
)
.catch((error) => console.log(`${error} did not connect`));
export default app;