-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (33 loc) · 926 Bytes
/
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
const express = require("express");
const employees = require("./employees");
const app = express();
const PORT = 3000;
app.use(express.json());
app.get("/employees/random", (req, res) => {
const randomIndex = Math.floor(Math.random() * employees.length);
const randomEmployee = employees[randomIndex];
console.log(randomEmployee);
if(!randomEmployee){
res.status(404).send("Random employee is not found!")
} else {
res.send(randomEmployee);
}
});
app.get("/employees/:id", (req, res) => {
const id = req.params.id;
const employee = employees.find((element)=> element.id == id);
if(!employee) {
res.status(404).send("Employee is not found!");
} else {
res.json(employee);
}
});
app.get("/employees", (req, res) => {
res.json(employees);
});
app.get("/", (req, res) => {
res.send("Hello employees!");
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT} `);
})