StudyCode
Читаем параметры из строки запроса (?q=node&limit=5) и фильтруем данные.
Query-параметры идут после ? в URL: /search?q=node&limit=5.
В Express они доступны через req.query:
const { q = '', limit = '10' } = req.query
Важно: все значения в req.query — строки. Число нужно конвертировать: parseInt(limit).
const ITEMS = ['Node.js', 'React', 'Vue', 'Express', 'Nodemon', 'Prisma'];
app.get('/search', (req, res) => {
const { q = '', limit = '10' } = req.query;
const filtered = filterItems(ITEMS, q); // ← шаг 1
const limited = filtered.slice(0, parseInt(limit));
const response = buildSearchResponse(limited, q, filtered.length); // ← шаг 2
res.json(response);
});{ q: "node", limit: "5" }items .filter(x => x.toLowerCase().includes(q.toLowerCase())) .slice(0, Number(limit));
{
"results": [
"Node.js",
"Nodemon"
],
"total": 2,
"query": "node"
}