I learn and play with Node and Express, creating a REST API. I do not have a database for storing data, I do everything in memory.
Let's say I have an array of users:
var users = [{"id": "1", "firstName": "John", "lastName": "Doe"}];
and defined the getAllUser function:
exports.getAllUser = function(page, items) {
page = (page < 1 ? 1 : page) || 1;
items = (items < 1 ? 5 : items) || 5;
var indexStart, indexEnd;
indexStart = (page - 1) * items;
indexEnd = indexStart + items;
return users.slice(indexStart, indexEnd);
};
and defined the route:
router.get('/users', function(req, res, next) {
var page = req.query.page;
items = req.query.items;
page = page !== 'undefined' ? parseInt(page, 10) : undefined;
items = items !== 'undefined' ? parseInt(items, 10) : undefined;
res.status(200).json({ users: users.search(page, items) });
});
This all works great, I was able to test it using Postman, and my data is being returned.
My question is: how to implement search and filtering?
From what I understand, search parameters will be passed to the URL as parameters, for example:
http://localhost:8080/api/users/firstName=john&age=30
How can I extract these parameters using node, and is there a specific library to use or best practices?
The same question for filtering or filtering the same as the search?