-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (42 loc) · 1.37 KB
/
app.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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const dotenv = require("dotenv");
const userRoutes = require("./routes/userRouters");
const adminUsersRoutes = require("./routes/adminRouters");
const itemRouters = require("./routes/itemRouters");
const orderRouters = require("./routes/orderRouters");
// const connectDB = require("./config/db");
const app = express();
// Middleware
app.use(express.json());
app.use(cors());
// connectDB();
dotenv.config();
mongoose
.connect("mongodb://localhost:27017/foodp")
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.error("Error connecting to MongoDB", err));
app.use("/api/users", userRoutes);
app.use("/api/admin", adminUsersRoutes);
app.use("/api/item", itemRouters);
app.use("/api/order", orderRouters);
app.use("/api/order", orderRouters);
//default page
app.get("/", (req, res) => {
res.send(
"<h1>Welcome to the Food Delivery App!</h1><p>Enjoy our service!</p>"
);
});
app.use((err, req, res, next) => {
const statusCode = res.statusCode === 200 ? 500 : res.statusCode;
res.status(statusCode);
res.json({
message: err.message,
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}/`);
});