-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_queryString_params.js
49 lines (41 loc) · 1.63 KB
/
5_queryString_params.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
42
43
44
45
46
47
48
49
const express = require('express');
const app = express();
const { products } = require('./data');
app.get('/', (req, res) => {
res.send('<h1>Home<ul><li><a href=/api/products>Products</a></li><li><a href=/api/product>Product</a></li></ul></h1>');
});
app.get('/api/products/', (req, res) => {
const filteredProducts = products.map(product => {
const { id, name, image } = product;
return { id, name, image };
});
res.json(filteredProducts);
});
app.get('/api/product/:productID', (req, res) => {
console.log(req.params.productID);
const singleProduct = products.find(product => product.id === Number(req.params.productID));
!singleProduct ? res.status(404).send('<h1>Product does not exist</h1>') : res.json(singleProduct);
});
app.get('/api/product/:id/test/:number', (req, res) => {
res.send(`<h1>hi ${Number(req.params.id)} and ${Number(req.params.number)}</h1>`);
});
app.get('/api/query', (req, res) => {
const { search, limit } = req.query;
let sortedPorduct = [...products];
if (search) {
sortedPorduct = sortedPorduct.filter(product => {
return product.name.startsWith(search);
});
}
if (limit) {
sortedPorduct = sortedPorduct.slice(0, Number(limit));
}
if (sortedPorduct.length < 1) {
// res.status(200).send(`<h1>etnter valid input</h1>`);
return res.status(200).json({ success: true, data: [] });
}
res.status(200).json(sortedPorduct);
});
app.listen(5000, () => console.log('port listening to server 5000'));
//always remember that querry strings will return values only in the string format
//for a req we should send only one data and if two data are send then error occurs