Open In App

How to implement search and filtering in a REST API with Node.js and Express.js ?

Last Updated : 06 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

An extremely popular way of implementing this is with the help of query strings. A query string is a part of the URL which 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. 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 on basis of these provided parameters, and then return the matching results.

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.

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

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

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!');
});


Examples

  1. URL: Fetch the user whose id is 2

    http://localhost:5000?id=2

    Output:

    [
       {
           "id": 2,
           "name": "Steve Rogers",
           "age": 106,
           "city": "Chicago"
       }
    ]
  2. URL: 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"
        }
    ]
  3. URL: 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"
       }
    ]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads