How to Implement Search and Filtering in a REST API with Node.js and Express.js ?
Last Updated :
10 Oct, 2024
Search and filtering are very basic features that an API must possess to serve data to the client application efficiently. By handling these operations on the server-side, we can reduce the amount of processing that has to be done on the client application, thereby increasing its performance.
In this article, we’ll see how we can build a Node.js REST API that can accept these query strings, filter a list of users based on these provided parameters, and then return the matching results.
Approach
An extremely popular way of implementing this is with the help of query strings. A query string is a part of the URL that allows us to pass data from client to server and vice-versa in the form of parameters and their values.
Syntax:
http://test.com?name=John&age=21
Here, the portion following the question mark (?) is the query string. These are basically key-value pairs that we can use for various purposes.
Then we will filter user data based on query parameters received in the request. We can iterate through the dataset, check if each user matches the specified filters, and return the filtered results as the response.
Also, to build powerful APIs that work seamlessly with modern frontends like React, the Full Stack Development with Node JS course covers API development in Node.js and Express.js with advanced features.
Setting up the Project
First, we’ve to initialize a new project using Node Package Manager. We can complete the setup by selecting all the default options.
npm init
Next, we’ve to install the express package.
npm install express --save
The entry point of this application is going to be the app.js file. All of our business logic will go here. The REST API will contain only a single route which will return a list of users, with support for searching and filtering using query strings.
Example: Initially, the app.js file will look something like this, with the route returning only a basic message.
JavaScript
// Filename - app.js
const express = require('express');
const data = require('./data');
// Initialize App
const app = express();
// Assign route
app.use('/', (req, res, next) => {
res.send('Node.js Search and Filter');
});
// Start server on PORT 5000
app.listen(5000, () => {
console.log('Server started!');
});
Adding Mock Data: For carrying out searching and filtering, we need some mock data i.e. a list of users upon which we can carry out these operations. For this, we can create a separate file data.js
JavaScript
// Filename - data.js
const data = [
{ id: 1, name: 'Alan Wake', age: 21, city: 'New York' },
{ id: 2, name: 'Steve Rogers', age: 106, city: 'Chicago' },
{ id: 3, name: 'Tom Hanks', age: 47, city: 'Detroit' },
{ id: 4, name: 'Ryan Burns', age: 16, city: 'New York' },
{ id: 5, name: 'Jack Ryan', age: 31, city: 'New York' },
{ id: 6, name: 'Clark Kent', age: 34, city: 'Metropolis' },
{ id: 7, name: 'Bruce Wayne', age: 21, city: 'Gotham' },
{ id: 8, name: 'Tim Drake', age: 21, city: 'Gotham' },
{ id: 9, name: 'Jimmy Olsen', age: 21, city: 'Metropolis' },
{ id: 10, name: 'Ryan Burns', age: 21, city: 'New York' },
];
module.exports = data;
Working with the Query String:
- Let’s consider this URL. Here, we want to fetch all the users who live in Metropolis and are 21 years old.
http://localhost:5000?city=Metropolis&age=21
- We can access the query string by using the query attribute of res object
console.log(res.query)
>> { city: 'Metropolis', age: '21' }
We can see that it contains all the parameters passed through the URL in form of key-value pairs. To apply these parameters on our list of users, we can use the Array.filter() method and check for each user, if it satisfies all the provided parameters, and if it does, then add it to the filteredUsers list.
The filteredUsers list is our final result and can be returned as the response. The final code is provided below
JavaScript
// Filename - App.js
const express = require('express');
const data = require('./data');
// Initialize App
const app = express();
// Assign route
app.use('/', (req, res, next) => {
const filters = req.query;
const filteredUsers = data.filter(user => {
let isValid = true;
for (key in filters) {
console.log(key, user[key], filters[key]);
isValid = isValid && user[key] == filters[key];
}
return isValid;
});
res.send(filteredUsers);
});
// Start server on PORT 5000
app.listen(5000, () => {
console.log('Server started!');
});
Example 1: URL to Fetch the user whose id is 2
http://localhost:5000?id=2
Output:
[
{
"id": 2,
"name": "Steve Rogers",
"age": 106,
"city": "Chicago"
}
]
Example 2: URL to Fetch all users who live in Metropolis
http://localhost:5000?city=Metropolis
Output:
[
{
"id": 6,
"name": "Clark Kent",
"age": 34,
"city": "Metropolis"
},
{
"id": 9,
"name": "Jimmy Olsen",
"age": 21,
"city": "Metropolis"
}
]
Example 3: URL to Fetch all users who live in Metropolis and are 21 years old
http://localhost:5000?city=Metropolis&age=21
Output
[
{
"id": 9,
"name": "Jimmy Olsen",
"age": 21,
"city": "Metropolis"
}
]
Summary
We can easy implement search and filter in REST API using node and express with the help to query string in the url. By handling query parameters and applying conditions to your database queries, you can create a flexible and powerful search and filtering system.