-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 1.72 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
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import mongoose from "mongoose";
import cookieParser from "cookie-parser";
// files
import AuthRoute from "./routes/authRoute.js";
import LiveRoute from "./routes/liveRoute.js";
import MasterRoute from "./routes/masterRoute.js";
import SalesRoute from "./routes/salesRoute.js";
import SalesReturnRoute from "./routes/salesReturnRoute.js";
import PurchaseRoute from "./routes/purchaseRoute.js";
import PurchaseReturnRoute from "./routes/purchaseReturnRoute.js";
const app = express();
dotenv.config();
const port = process.env.PORT || 5000;
const connect = async () => {
try {
mongoose.connect(process.env.MONGO);
console.log("Connected to MongoDB");
} catch (err) {
throw err;
}
};
// middlewares
app.use(cors());
app.use(cookieParser());
app.use(express.json({ limit: "50mb" }));
app.use(
express.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 })
);
// API endpoints
app.use("/api/auth", AuthRoute);
app.use("/api/livestock", LiveRoute);
app.use("/api/master", MasterRoute);
app.use("/api/purchase", PurchaseRoute);
app.use("/api/purchaseReturn", PurchaseReturnRoute);
app.use("/api/sales", SalesRoute);
app.use("/api/salesReturn", SalesReturnRoute);
// error handling
app.use((err, req, res, next) => {
const errorStatus = err.status || 500;
const errorMessage = err.message || "Something went wrong";
return res.status(errorStatus).json({
success: false,
status: errorStatus,
message: errorMessage,
stack: err.stack,
});
});
// Getting some confirmation message
app.use("/", (req, res) => {
res.send("Backend working");
});
app.listen(port, () => {
connect();
console.log("Connected to backend");
});