-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock-server.js
More file actions
50 lines (40 loc) · 1.21 KB
/
mock-server.js
File metadata and controls
50 lines (40 loc) · 1.21 KB
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
import express, { json } from "express";
import cors from "cors";
const app = express();
const PORT = 4000;
app.use(cors());
app.use(json());
app.get("/api/users", (req, res) => {
const requestedPage = parseInt(req.query.pageNumber);
const requestedPageSize = parseInt(req.query.pageSize);
const pageNumber = isNaN(requestedPage) ? 1 : requestedPage;
const pageSize = isNaN(requestedPageSize) ? 10 : requestedPageSize;
if (pageNumber < 1 || pageSize < 1) {
return res.status(400).json({
error:
"Invalid pagination parameters: pageNumber and pageSize must be >= 1.",
});
}
const totalElements = 200;
const totalPages = Math.ceil(totalElements / pageSize);
const startIndex = (pageNumber - 1) * pageSize;
const endIndex = Math.min(startIndex + pageSize, totalElements);
const users = Array.from({ length: endIndex - startIndex }, (_, i) => {
const id = startIndex + i + 1;
return {
id: id,
firstName: `User ${id}`,
lastName: `Lastname ${id}`,
};
});
res.json({
pageNumber,
pageSize,
totalElements,
totalPages,
content: users,
});
});
app.listen(PORT, () => {
console.log(`Mock server running at http://localhost:${PORT}`);
});