-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
77 lines (67 loc) · 2.04 KB
/
app.ts
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
import express, { NextFunction, Request, Response } from "express";
import morgan from "morgan";
import userRouter from "./src/routes/useRouter";
import BlogRouter from "./src/routes/blogsRoute";
import messageRouter from "./src/routes/messageRoute";
import { addComment, getComments } from "./src/controllers/commentController";
const authController = require("./src/controllers/authController");
import swaggerUi from "swagger-ui-express";
import fs from "fs";
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import cors from 'cors'
import cookieParser from 'cookie-parser'
import Document from "./swagger.json";
import validateComments from "./src/validations/commentValidation";
const customCss = fs.readFileSync(process.cwd() + "/swagger.css", "utf8");
const app = express();
dotenv.config({ path: 'config.env' })
export interface EnvConfig {
PORT: string | number
DATABASE: string
DATABASE_PASSWORD: string
}
const { PORT, DATABASE } =
process.env as unknown as EnvConfig
const DB = DATABASE
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
.then(() => {
console.log('DB connection established')
})
.catch((err) => {
console.error('DB connection failed:', err)
})
app.listen(PORT, () => {
console.log(`App running on port ${PORT}...`)
})
app.use(morgan("dev"));
app.use(express.json());
app.use(cookieParser());
app.use(cors())
app.use((req:Request, res:Response,next:NextFunction) => {
console.log(req.cookies);
next()
})
app.get('/',(req,res)=>{
res.status(200).json({
message: "Welcome to the API"
})
})
app.use("/api/messages", messageRouter);
app.use("/docs", swaggerUi.serve, swaggerUi.setup(Document, { customCss }));
app.use("/api/users", userRouter);
app.use("/api/blog", BlogRouter);
app.post(
"/api/:id/comment",
validateComments,
authController.protect,
addComment
);
app.get("/api/:id/comment", authController.protect, getComments);
export default app;