-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (47 loc) · 1.44 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
require("dotenv").config();
const express = require("express");
const cors = require("cors"); // Import the cors middleware
const app = express();
const port = process.env.PORT || 5000;
// Use cors middleware
app.use(cors());
const path = require("path");
// Parse JSON requests
app.use(express.json());
const mongoose = require("mongoose");
const categoryRoute = require("./route/categoryRoute");
const orderRoute = require("./route/orderRoute");
const productRoute = require("./route/productRoute");
const userRoute = require("./route/userRoute");
app.use("/api/category", categoryRoute);
app.use("/api/order", orderRoute);
app.use("/api/product", productRoute);
app.use("/api/user", userRoute);
// app.use(require("./try"));
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: process.env.MONGO_DATABASE,
});
mongoose.connection.on("connected", () => {
console.log(`Connected to MongoDB`);
});
mongoose.connection.on("error", (err) => {
console.error(`Failed to connect to MongoDB: ${err}`);
});
// Serve the frontend
app.use(express.static(path.join(__dirname, "billing-system/build")));
app.get("*", (req, res) => {
res.sendFile(
path.join(__dirname, "billing-system/build/index.html"),
function (err) {
if (err) {
res.status(500).send(err);
}
}
);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});