-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (72 loc) · 2.24 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();
const { PORT, MONGODB_URI, ORIGIN, NODE_ENV } = require("./config");
const { API_ENDPOINT_NOT_FOUND_ERR, SERVER_ERR } = require("./errors");
// routes
const userAuthRoutes = require("./routes/userAuth.route");
const vendorAuthRoutes = require("./routes/vendorAuth.route");
const adminAuthRoutes = require("./routes/adminAuth.route");
const deliveryPartnerRoutes = require("./routes/deliverPartnerAuth.route")
// init express app
const app = express();
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
// res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
app.use(express.json());
// index route
app.get("/", (req, res) => {
res.status(200).json({
type: "success",
message: "server is up and running",
data: null,
});
});
// routes middlewares
app.use("/api/v1/auth/users", userAuthRoutes);
app.use("/api/v1/auth/vendors", vendorAuthRoutes);
app.use("/api/v1/auth/admins", adminAuthRoutes);
app.use("/api/v1/auth/delivery_partner",deliveryPartnerRoutes )
// page not found error handling middleware
app.use("*", (req, res, next) => {
const error = {
status: 404,
message: API_ENDPOINT_NOT_FOUND_ERR,
};
next(error);
});
// global error handling middleware
app.use((err, req, res, next) => {
console.log(err);
const status = err.status || 500;
const message = err.message || SERVER_ERR;
const data = err.data || null;
res.status(status).json({
type: "error",
message,
data,
});
});
async function main() {
try {
await mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// console.log("database connected");
// console.log(NODE_ENV);
if (NODE_ENV != "test") {
app.listen(PORT, () => console.log(`Server listening on port ${PORT}`));
}
} catch (error) {
console.log(error);
process.exit(1);
}
}
main();
module.exports = app;