-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (32 loc) · 1.2 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
// Import required modules
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const Product = require("./models/product.model.js"); // Import the Product model
const productRoute = require("./routes/product.route.js");
dotenv.config(); // Load environment variables from .env file
const app = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Routes
app.use("/api/products", productRoute);
// Centralized error handler middleware
app.use((err, req, res, next) => {
console.error(err.stack); // Log error details
res.status(500).json({ message: "Something went wrong!" }); // Send generic error response
});
// Connect to MongoDB and start the server
mongoose
.connect(process.env.MONGO_URI) // Use environment variable for MongoDB URI
.then(() => {
console.log("Connected to the Database");
// Use environment variable for PORT or default to 3000
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
})
.catch((err) => {
console.log("Failed to connect to the Database", err); // Log connection errors
});