-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
69 lines (57 loc) · 1.99 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//THIS FILE IS WHERE THE FUNCTIONS FROM THE OTHER FILES ARE LINKED
// Import the required modules
import express from "express";
import { pool } from "./db/index.js";
import {
getProducts,
getBlueProducts,
getRedProducts,
getGreenProducts,
} from "./database_files/products.js";
import cors from "cors";
//Initialise the express app
const app = express();
app.use(cors());
// // Retrieve the port number from environment variables
const PORT = process.env.PORT;
app.use(express.json()); // express.json() middleware is used to parse incoming JSON requests
//Retrieving all the products from the products table
app.get("/products", async function (req, res) {
console.log("Hello Products!");
let products = await getProducts();
res.status(200).json({ status: "success", payload: products });
});
//Retrieving all the blue products from the products table
app.get("/products/blue", async function (req, res) {
try {
console.log("Hello blue products");
let products = await getBlueProducts();
res.status(200).json({ status: "success", payload: products });
} catch (error) {
res.json({ success: false, message: error.message });
}
});
//Retrieving all the red products from the products table
app.get("/products/red", async function (req, res) {
try {
console.log("Hello red products");
let products = await getRedProducts();
res.status(200).json({ status: "success", payload: products });
} catch (error) {
res.json({ success: false, message: error.message });
}
});
//Retrieving all the green products from the products table
app.get("/products/green", async function (req, res) {
try {
console.log("Hello green products");
let products = await getGreenProducts();
res.status(200).json({ status: "success", payload: products });
} catch (error) {
res.json({ success: false, message: error.message });
}
});
// Start the server and listen on the specified port
app.listen(3000, function () {
console.log(`Server listening on port ${PORT}`);
});