-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
39 lines (32 loc) · 1.09 KB
/
routes.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
const express = require("express");
const employees = require("./employees");
const router = express.Router();
router.get("/", (req, res) => {
res.json(employees);
});
router.get("/random", (req, res) => {
const i = Math.floor(Math.random() * employees.length);
res.json(employees[i]);
});
router.get("/:id", (req, res) => {
const id = +req.params.id;
const employee = employees.find(emp => emp.id === id);
if (employee) {
res.json(employee); // Send the employee as JSON
} else {
res.status(404).send(`There is no employee with id ${id}.`); // Send a 404 response if not found
}
});
router.post("/", (req, res) => {
const { name } = req.body;
if (!name || typeof name !== "string" || name.trim() === "") {
return res.status(400).send("name is required girl...");
}
const newEmployee = {
id: employees.length > 0 ? employees[employees.length - 1].id + 1 : 1,
name: name.trim
}
employees.push(newEmployee);
res.status(201).json(newEmployee);
});
module.exports = router;