-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (49 loc) · 1.45 KB
/
server.js
File metadata and controls
58 lines (49 loc) · 1.45 KB
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
const express = require("express");
const mongoose = require("mongoose");
const Product = require("./models/product_model");
require("dotenv").config(); // Load environment variables from .env file
const MONGODB_URL = process.env.MONGODB_URL; // Get MongoDB URL from environment variables
const app = express();
// Middleware
app.use(express.json());
// Routes
app.get("/", (req, res) => {
res.send("this is the homepage of the Website");
});
// About Page
app.get("/about", (req, res) => {
res.send(
"https://drive.google.com/file/d/0B-Bn8BSb6ghDc3BuTlFiSF9xZ09TekVrcm9mLWw5anpRRGNV/view?usp=sharing&resourcekey=0-lKwU6HZ_h4l6MvR2RJgNlQ"
);
});
// Get all products
app.get("/products", async (req, res) => {
try {
const products = await Product.find({});
res.status(200).json({ results: products });
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Create a new product
app.post("/products", async (req, res) => {
try {
const product = await Product.create(req.body);
res.status(200).json(product);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running at port ${PORT}`);
});
mongoose
.connect(MONGODB_URL)
.then(() => {
console.log("DB Connected Successfully");
})
.catch((err) => {
console.error("Error connecting to DB:", err);
});