-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.basic.js
52 lines (44 loc) · 1.46 KB
/
app.basic.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
const express = require("express");
const { users, expenses } = require("./db");
const app = express();
app.use(express.json());
app.use((req, res, next) => {
const user = users.find((u) => u.id === req.headers["authorization"]);
if (!user) {
return res.status(401).json({ error: "Unauthorized" });
} else {
req.user = user;
next();
}
});
app.get("/", (req, res) => {
res.json({
message: "CerbFinance is running",
});
});
app.get("/expenses/:id", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
res.json(expense);
});
app.patch("/expenses/:id", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
// do the patch here
res.json(expense);
});
app.post("/expenses/:id/approve", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
// do the approve here
res.json(expense);
});
app.delete("/expenses/:id", (req, res) => {
const expense = expenses.find((expense) => expense.id === req.params.id);
if (!expense) return res.status(404).json({ error: "Expense not found" });
// do the delete here
res.json(expense);
});
app.listen(8000, () => {
console.log("Server is running on port 8000");
});