Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (79 loc) · 1.63 KB
/
Copy pathserver.js
File metadata and controls
89 lines (79 loc) · 1.63 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require('express');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3001;
app.use(cors());
const DATA = [
'apple',
'apricot',
'banana',
'banana1',
'banana2',
'banana3',
'banana4',
'blueberry',
'blackberry',
'cherry',
'date',
'dragonfruit',
'fig',
'grape',
'grape1',
'grape2',
'grape3',
'grapefruit',
'kiwi',
'lemon',
'lime',
'mango',
'nectarine',
'orange',
'papaya',
'peach',
'pear',
'pineapple',
'plum',
'raspberry',
'strawberry',
'watermelon',
'watermelon1',
'watermelon2',
'watermelon3',
'watermelon4',
'watermelon5',
'watermelon6',
'watermelon7',
'watermelon8',
'watermelon9',
'watermelon10',
'watermelon11',
'watermelon12',
'watermelon13',
];
app.get('/api/search', (req, res) => {
const query = (req.query.q || '').toLowerCase();
const page = parseInt(req.query.page) || 1;
const perPage = parseInt(req.query.perPage) || 10;
// Server side error
if (query == '500') {
return res.status(500).json({ error: 'Internal Server Error' });
}
// Response delay
let delay = 300;
if (query == 'banan') {
delay = 6000;
}
setTimeout(() => {
if (!query) {
return res.json({ results: [] });
}
const results = DATA.filter((item) => item.includes(query));
const startIndex = (page - 1) * perPage;
const endIndex = startIndex + perPage;
const paginatedResults = results.slice(startIndex, endIndex);
res.json({ results: paginatedResults });
}, delay);
});
app.listen(port, () => {
console.log(`🚀 Mock autocomplete API running at http://localhost:${port}`);
});