-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 1.26 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
56
import { config } from "dotenv";
import express from "express";
import UserAgent from "express-useragent";
import { connectDB } from "./db/database.js";
import authRoute from "./routes/auth.js";
import jobsRoute from "./routes/jobs.js";
import userRoute from "./routes/user.js";
import HttpStatus from "./utils/HttpStatus.js";
config();
const app = express();
const port = process.env.PORT || 8000;
// Steup
app.use(express.json());
app.use(UserAgent.express());
// Cros
app.use((_req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
// Routes
app.use("/auth", authRoute);
app.use("/", jobsRoute);
app.use("/user", userRoute);
// Error Handling
app.use((error, _req, res, _next) => {
console.error(error);
const status = error.statusCode || 500;
const msg = error.message;
const data = error.data;
res.status(status).json({
status: HttpStatus.ERROR,
data: {
msg,
data,
},
});
});
connectDB()
.then(() => {
app.listen(port, () => {
console.log(`listening: ${port}`);
});
})
.catch((err) => console.log(err));