-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (43 loc) · 1.3 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
const express = require("express")
const dotenv = require("dotenv")
const chalk = require("chalk")
const cors = require("cors")
const path = require("path")
dotenv.config({ path: "./config/config.env" })
const db = require("./config/db")
const routes = require("./routes")
db.connectDb()
const app = express()
const PORT = process.env.PORT
const env = process.env.NODE_ENV
const green = chalk.green.bold
const cyan = chalk.cyan.bold
const red = chalk.red.bold
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, "public")))
app.use(routes)
app.use((error, req, res, next) => {
if (error.response) {
res.status(error.response.status || 500).json({
message: error.response.data === " " ? "Something Went Wrong" : error.response.data,
stack: env === "production" ? "🍔" : error.stack
})
} else {
res.status(error.status || 500).json({
message: error.message,
stack: env === "production" ? "🍔" : error.stack
})
}
})
process.on("unhandledRejection", (reason, p) => {
throw reason
})
process.on("uncaughtException", (error) => {
console.log(red("Uncaught Exception ✗", error))
process.exit(1)
})
app.listen(PORT, () =>
console.log(`${green("✓")} ${cyan(` App is running at port ${PORT} in ${env} mode.`)}`)
)