-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (37 loc) · 963 Bytes
/
server.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
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cors from "cors";
import { ERROR } from "./src/constant.js";
const app = express();
const PORT = process.env.NODE_ENV || 8000;
//connect to database
import { connectDB } from "./src/congfig/dbCongif.js";
connectDB();
//middlewares
app.use(express.json());
app.use(cors());
// api routers
import userRouter from "./src/router/userRouter.js";
app.use("/api/v1/user", userRouter);
// all uncaught request
app.use("*", (req, res) => {
res.json({
message: "System status is healthy!",
});
});
//global error handler
app.use((error, req, res, next) => {
console.log(error.message);
const errorCode = error.errorCode || 500;
res.status(errorCode).json({
status: ERROR,
message: error.message,
});
});
//run the server
app.listen(PORT, (error) => {
error
? console.log(error)
: console.log(`server is running at http://localhost:${PORT}`);
});