...

/

Create the Search Repository Route

Create the Search Repository Route

Learn about the GitHub search repositories API, and implement the Express.js API that fetches the count of repositories using Redis.

This is the beginning of our GitHub search project. As discussed earlier, we’ll have three functionalities in our application. We’re going to implement the first functionality to create an API endpoint that searches the GitHub repositories based on the search term provided by the user. However, before implementing our API to search GitHub repositories, it’s good to understand what the repositories exactly mean.

Press + to interact

A GitHub repository, in short, is a storage space where we can organize, store, and manage our project's code and related files. It serves as a centralized location for all the files, folders, and version history of our project. With a GitHub repository, we can easily collaborate with others by sharing our code, tracking changes, and incorporating contributions from different team members. It provides version control capabilities, allowing us to keep track of modifications made to our codebase over time. We can create branches to work on different features or fixes and then merge them back into the main branch. In addition to code, repositories can contain documentation, images, configuration files, and other resources relevant to our project.

Explore the GitHub search repository API

Before moving on, let’s first do some exploration of the GitHub search repository API using the axios module.

Press + to interact
// Importing the reuqired module
const axios = require('axios')
// Function to call the GitHub API and print the response containing the repositories
const fetchGitHubRepo = async () => {
const GITHUB_SEARCH_REPO_URL = "https://api.github.com/search/repositories?q="
const searchTerm = "redis"
// Calling the GitHub API
const response = await axios.get(GITHUB_SEARCH_REPO_URL + searchTerm)
// Printing the response received
console.log(response.data)
}
// Calling the async function
fetchGitHubRepo()

Code explanation:

Note: Observe that the output contains the total number of results fetched (which we’ll use in our final app), a flag denoting whether the results are incomplete due to a timeout issue (which can happen when there are a lot of records to fetch or the servers are experiencing heavy load), and the actual result as items, which consist of the repository information. Try changing the searchTerm and observe the different results.

Lines 5–15: We create an async function, fetchGitHubRepo(), that calls the GitHub API with the search term and prints ...