<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Stephendache</title>
    <description>The latest articles on Forem by Stephendache (@stephendache).</description>
    <link>https://forem.com/stephendache</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1019859%2F8636ba27-7688-40f7-9e2c-857b032efd32.jpg</url>
      <title>Forem: Stephendache</title>
      <link>https://forem.com/stephendache</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stephendache"/>
    <language>en</language>
    <item>
      <title>Building a Node.js Application to Search and Display GitHub Profiles Using GitHub API</title>
      <dc:creator>Stephendache</dc:creator>
      <pubDate>Sat, 08 Apr 2023 21:32:09 +0000</pubDate>
      <link>https://forem.com/stephendache/building-a-nodejs-application-to-search-and-display-github-profiles-using-github-api-4kc2</link>
      <guid>https://forem.com/stephendache/building-a-nodejs-application-to-search-and-display-github-profiles-using-github-api-4kc2</guid>
      <description>&lt;p&gt;Are you looking to build your first Node.js application with GitHub API? In this tutorial, I will show you how to build a very simple application that allows you to search for GitHub users and display their profile information using the GitHub API.&lt;/p&gt;

&lt;p&gt;To get started, you will need to have Node.js installed on your computer or laptop. If you don't have them installed, you can download them from the Node.js website or following the installation step on &lt;a href="https://www.digitalocean.com/community/tutorial_collections/how-to-install-node-js" rel="noopener noreferrer"&gt;Digital Ocean.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, let's create a new Node.js project by running the following commands in your terminal or git bash CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; mkdir node-github-search
&amp;gt;&amp;gt; cd node-github-search
&amp;gt;&amp;gt; npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's install the required packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express ejs axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't get frightned, this is what each packages is resposible for&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;express&lt;/code&gt; is a Node.js web application framework that allows you to create web applications easily.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ejs&lt;/code&gt; is a templating engine that allows you to generate HTML dynamically based on data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;axios&lt;/code&gt; is a library that allows you to make HTTP requests to APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have our project set up and the necessary packages installed, let's create our app.js file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const axios = require('axios')
const app = express()

app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: false }))
app.use(express.static('public'))

app.get('/', (req, res) =&amp;gt; {
  res.render('index')
});

app.post('/users', (req, res) =&amp;gt; {
  const username = req.body.username
  axios.get(`https://api.github.com/users/${username}`)
    .then(response =&amp;gt; {
      const user = response.data
      res.render('users', { user })
    })
    .catch(error =&amp;gt; {
      console.log(error)
      res.render('error')
    })
})

app.listen(3000, () =&amp;gt; {
  console.log('Server started on port 3000')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we first import the required packages and create an instance of our express application. We then set the view engine to ejs, which allows us to use &lt;code&gt;ejs&lt;/code&gt; to render our HTML.&lt;/p&gt;

&lt;p&gt;Next, we set up two routes: one for the homepage &lt;code&gt;(/)&lt;/code&gt; and one for the user search (/users). In the homepage route, we simply render an index.ejs file that contains a form for searching for users. In the user search route, we get the username from the form data, use the axios library to make a GET request to the GitHub API, and then render a &lt;code&gt;users.ejs&lt;/code&gt; file that displays the user's profile information.&lt;/p&gt;

&lt;p&gt;Now let's create our views folder and create an index.ejs file inside of the views:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;Github User Search&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Github User Search&amp;lt;/h1&amp;gt;
    &amp;lt;form action="/users" method="POST"&amp;gt;
      &amp;lt;label for="username"&amp;gt;Enter a Github username:&amp;lt;/label&amp;gt;
      &amp;lt;input type="text" id="username" name="username"&amp;gt;
      &amp;lt;button type="submit"&amp;gt;Search&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we simply create a form that allows &lt;code&gt;users&lt;/code&gt; to enter a GitHub username and search for it.&lt;/p&gt;

&lt;p&gt;Finally, let's create our users.ejs file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;title&amp;gt;&amp;lt;%= user.name %&amp;gt; - Github User Profile&amp;lt;/title&amp;gt;
 &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;&amp;lt;%= user.name %&amp;gt;&amp;lt;/h1&amp;gt;
    &amp;lt;img src="&amp;lt;%= user.avatar_url %&amp;gt;" alt="Profile Picture"&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Username:&amp;lt;/strong&amp;gt; &amp;lt;%= user.login %&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Bio:&amp;lt;/strong&amp;gt; &amp;lt;%= user.bio %&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Followers:&amp;lt;/strong&amp;gt; &amp;lt;%= user.followers %&amp;gt;&amp;lt;/li&amp;gt;
      &amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Following:&amp;lt;/strong&amp;gt; &amp;lt;%= user.following %&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use &lt;code&gt;ejs&lt;/code&gt; templating to dynamically generate HTML based on the user object we passed in. We display the user's name, profile picture, username, bio, followers, and following count.&lt;/p&gt;

&lt;p&gt;Now that we have all of our files set up, we can start our server by running node &lt;code&gt;app.js&lt;/code&gt; in the terminal. Then, open your web browser and navigate to &lt;code&gt;http://localhost:3000&lt;/code&gt;. You should see the homepage with a search form. Try entering a GitHub username and clicking the search button. You will be taken to a page that displays the user's profile information.&lt;/p&gt;

&lt;p&gt;Congratulations! You've just built your first Node.js application that uses the GitHub API to search for users and display their profile information. With the knowledge you've gained from this tutorial, you can now start exploring and building more complex Node.js applications.&lt;/p&gt;

&lt;p&gt;Keep grinding, connect with me on &lt;a href="https://twitter.com/iamstepaul" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
