-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (34 loc) · 1.06 KB
/
server.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
// TODO: this file :)
const express = require("express");
const employees = require("./employees");
const app = express();
const init = () => {
app.listen(3001, () => console.log("im listening"));
};
app.get("/", async (req, res) => {
res.status(200).send("Welcome");
});
app.get("/employees", (req, res) => {
res.status(200).json(employees);
});
app.get("/employees/:id", (req, res) => {
const id = req.params.id;
const employee = employees.find((emp) => emp.id == id);
if (employee) {
res.status(200).json(employee);
} else {
res.status(404).json({ message: "employee not found" });
}
});
app.get("/employees/random", (req, res) => {
const randomIndex = Math.floor(Math.random() * employees.length);
console.log("Random Index:", randomIndex);
const randomEmployee = employees[randomIndex];
console.log("Random Employee:", randomEmployee);
if (randomEmployee) {
res.status(200).json(randomEmployee); // Respond with the random employee
} else {
res.status(404).json({ message: "Employee not found" }); // Handle error
}
});
init();