-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (35 loc) · 952 Bytes
/
app.js
File metadata and controls
47 lines (35 loc) · 952 Bytes
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
// app.js
const express = require("express");
const { coffees, orders } = require("./data");
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.static("public"));
module.exports = app;
// Endpoint to fetch available coffees
app.get("/coffees", (req, res) => {
res.json(coffees);
});
// Endpoint to place an order
app.post("/order", (req, res) => {
const { coffeeId, quantity } = req.body;
const coffee = coffees.find((c) => c.id === coffeeId);
if (!coffee) {
return res.status(400).json({ error: "Invalid coffee ID" });
}
const order = {
orderId: orders.length + 1,
coffeeName: coffee.name,
quantity,
total: coffee.price * quantity,
};
orders.push(order);
res.status(201).json(order);
});
// Endpoint to fetch all orders
app.get("/orders", (req, res) => {
res.json(orders);
});
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
});