forked from Pratham-Mishra04/ForkThis-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (42 loc) · 1.49 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
import express from "express";
import morgan from "morgan"
import path from 'path'
import AppError from "./managers/AppError.js";
import { noURL } from "./controllers/errorController.js";
import userRouter from "./routers/userRouter.js";
import helmet from "helmet";
import ExpressMongoSanitize from "express-mongo-sanitize";
import cors from 'cors'
import connectToDB from './managers/DB.js';
import {uncaughtExceptionManager, unhandledRejectionManager} from './managers/baseErrorManager.js';
import githookRouter from "./routers/githookRouter.js";
import challengeRouter from "./routers/challengeRouter.js";
import shopRouter from "./routers/shopRouter.js";
import envHandler from "./managers/envHandler.js";
uncaughtExceptionManager
const __dirname=path.resolve()
const app=express()
app.use(express.json())
app.use(cors())
app.use(helmet())
app.use(ExpressMongoSanitize())
app.use(express.static(path.join(__dirname, 'public')))
if(envHandler('NODE_ENV')=='dev') app.use(morgan("dev"))
connectToDB()
app.listen(process.env.PORT, () => {
console.log(`Server is running on http://127.0.0.1:${process.env.PORT}`);
});
unhandledRejectionManager
app.use((req,res,next)=>{
req.requestedAt=new Date().toISOString();
next()
})
app.use("/users", userRouter)
app.use("/githubPayload", githookRouter)
app.use("/challenge", challengeRouter)
app.use("/shop", shopRouter)
app.all("*", (req, res, next)=>{
next(new AppError(`Cannot find ${req.originalUrl}`, 404))
})
app.use(noURL)
export default app