-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (41 loc) · 1.25 KB
/
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
47
48
49
50
51
import "express-async-errors"; // best at the very top
import * as dotenv from "dotenv"; // best at the very top
dotenv.config();
import express from "express";
import morgan from "morgan";
import jobRouter from "./routes/jobRouter.js";
import authRouter from "./routes/authRouter.js";
import mongoose from "mongoose";
import errorHandlerMiddleware from "./middleware/errorHandlerMiddleware.js";
const app = express();
app.use(express.json());
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use("/api/v1/jobs", jobRouter);
app.use("/api/v1/auth", authRouter);
app.get("/", (req, res) => {
res.send("Hello World");
});
app.post("/", (req, res) => {
console.log("req", req);
res.json({ message: "data received", data: req.body });
});
// middleware for handling wrong url request (not found)
app.use("*", (req, res) => {
res.status(404).json({
msg: "url not found",
});
});
// middleware error route for errors during processing
app.use(errorHandlerMiddleware);
const port = process.env.PORT || 5000;
try {
await mongoose.connect(process.env.MONGO_URI);
app.listen(4000, () => {
console.log(`Server is working on port ${port}`);
});
} catch (error) {
console.log("DB Connection Error", error);
process.exit(1);
}