<?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>DEV Community: mech2dev</title>
    <description>The latest articles on DEV Community by mech2dev (@danielstai).</description>
    <link>https://dev.to/danielstai</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%2F874938%2F40266fd2-5756-49c8-a371-35b71632e2b8.png</url>
      <title>DEV Community: mech2dev</title>
      <link>https://dev.to/danielstai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danielstai"/>
    <language>en</language>
    <item>
      <title>Deploying your API to the web</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Mon, 19 Dec 2022 11:23:33 +0000</pubDate>
      <link>https://dev.to/danielstai/deploying-your-api-to-the-web-107o</link>
      <guid>https://dev.to/danielstai/deploying-your-api-to-the-web-107o</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In previous articles, we have built APIs and tested them in our local environment. In this article we will deploy a back-end to the internet.&lt;/p&gt;

&lt;p&gt;Here's the project we will be deploying:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DanielStai" rel="noopener noreferrer"&gt;
        DanielStai
      &lt;/a&gt; / &lt;a href="https://github.com/DanielStai/jwt_auth" rel="noopener noreferrer"&gt;
        jwt_auth
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Authentication in an Express app&lt;/h2&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Implementing user authentication in an express app.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Technologies used&lt;/h2&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Authentication done using &lt;code&gt;jwt (json web token)&lt;/code&gt;. Passwords are hashed using &lt;code&gt;bcrypt&lt;/code&gt;. The API uses a MongoDB database.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Endpoints&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/users/&lt;/td&gt;
&lt;td&gt;POST  (public)&lt;/td&gt;
&lt;td&gt;register a user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/users/login&lt;/td&gt;
&lt;td&gt;POST (public)&lt;/td&gt;
&lt;td&gt;logs in a user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/users/me&lt;/td&gt;
&lt;td&gt;GET (private)&lt;/td&gt;
&lt;td&gt;gets the info of the logged in user&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;How to run&lt;/h2&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Clone the repository on your local computer&lt;/li&gt;
&lt;li&gt;Do &lt;code&gt;npm run install&lt;/code&gt; to install the required dependencies&lt;/li&gt;
&lt;li&gt;Set up your MongoDB collection&lt;/li&gt;
&lt;li&gt;Do &lt;code&gt;npm run dev&lt;/code&gt; to start the API&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Test the API with Postman&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DanielStai/jwt_auth" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;We will use Railway as our preferred hosting provider. There are many considerations when choosing a provider. Some considerations include cost, ease of scaling, customer support amongst many more. &lt;/p&gt;

&lt;p&gt;Why Railway?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;They provide a free basic tier which is perfect for learners who want to experiment and learn how to deploy our projects. For more traffic heavy applications, you may have to upgrade your plan. Read more about Railway &lt;a href="https://railway.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Railway makes deployment for learners easy since it takes care of most of the infrastructure (servers, load balancing, reverse proxies etc) behind the scenes. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The skills acquired when deploying on Railway are transferable and can be used on other popular hosting sites&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Preparing for deployment
&lt;/h2&gt;

&lt;p&gt;So far, we have been working on our projects in our local environment. We now want to deploy our API to the web and to do so, we need to make some changes to our API to make it 'production ready'.&lt;/p&gt;

&lt;p&gt;First we will set NODE_ENV to 'production' in our &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;This will result in less verbose error messages being displayed to the user of the API, increasing efficiency. &lt;/p&gt;

&lt;p&gt;This error handling feature is defined in our error handling middleware (in this case it's in the file &lt;code&gt;jwt_auth/middleware/errorMiddleware.js&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Second we will install &lt;code&gt;compression&lt;/code&gt; and &lt;code&gt;helmet&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Compression enables us to compress the response sent back to the client making our app more efficient. Once installed, we will use compression in the root of our project as we do with other middleware.&lt;/p&gt;

&lt;p&gt;We will also install &lt;code&gt;helmet&lt;/code&gt; in the root of our project. Helmet provides additional HTTP headers that protect our app from some vulnerabilities. &lt;/p&gt;

&lt;p&gt;To install both helmet and compression, run &lt;code&gt;npm install helmet compression&lt;/code&gt;. Import and chain to your project in &lt;code&gt;server.js&lt;/code&gt; as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;helmet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;compression&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;extended&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also make some changes to how we handle logging in our project but that will be covered in a later article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a new project on Railway
&lt;/h2&gt;

&lt;p&gt;Our project is ready to be deployed. Let's head over to the Railway website.&lt;/p&gt;

&lt;p&gt;Create an account on Railway and start a new project by clicking on new project:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzwabnwxcene2mqqbm1h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzwabnwxcene2mqqbm1h.png" alt="new Railway project" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on deploy from GitHub repo and install the Railway app to your GitHub account:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jf73xl1xf3cbgsiqtn5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jf73xl1xf3cbgsiqtn5.png" alt="deploy from github repo" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select on the repos you gave railway access to. In my case, this is the &lt;code&gt;jwt_auth&lt;/code&gt; repo:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiqbi4qzk972enrt8tpi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foiqbi4qzk972enrt8tpi.png" alt="select repo" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on deploy now and wait for the project to build and deploy. You might get an error since we haven't set our environment variables yet:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9esnlmm5i2e2clxvihwf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9esnlmm5i2e2clxvihwf.png" alt="deploy now" width="571" height="607"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set environment variables. Remember these are the variables we had defined in our .env file:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobfuj5k7fsia4uw0mk0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobfuj5k7fsia4uw0mk0h.png" alt="environment variables" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait for the build and deploy process to finish and if successful, you will get a success message as below:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd6aftm8g17v5855o0ml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnd6aftm8g17v5855o0ml.png" alt="succesful deployment" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since our aim is to expose our API to the world, we then generate a domain name for our API:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5g9vcww84ktyakuujsde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5g9vcww84ktyakuujsde.png" alt="generating a domain" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what we get when we click on generate domain:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F505oou29xa2o8oo7mw10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F505oou29xa2o8oo7mw10.png" alt="domain generated" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that done, we have successfully deployed our API. You can monitor your deployment logs every once in a while since if an error occurs you can get the details there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have successfully build an API with jwt authentication and deployed it to the web. In the next article, we will test our API and debug some of the common issues encountered when deploying.&lt;/p&gt;

</description>
      <category>portfolio</category>
      <category>webdev</category>
      <category>design</category>
    </item>
    <item>
      <title>User authentication in an Express app</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Mon, 19 Dec 2022 10:20:01 +0000</pubDate>
      <link>https://dev.to/danielstai/user-authentication-in-an-express-app-1lgf</link>
      <guid>https://dev.to/danielstai/user-authentication-in-an-express-app-1lgf</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this article we will delve into authentication using json web token (JWT). User authentication is the process of verifying a user attempting access to an application or part of an application. &lt;/p&gt;

&lt;p&gt;In the API we will build, we will implement authentication by building middleware and using the middleware to protect the routes we need to limit access to.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DanielStai" rel="noopener noreferrer"&gt;
        DanielStai
      &lt;/a&gt; / &lt;a href="https://github.com/DanielStai/jwt_auth" rel="noopener noreferrer"&gt;
        jwt_auth
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Authentication in an Express app&lt;/h2&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Implementing user authentication in an express app.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Technologies used&lt;/h2&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Authentication done using &lt;code&gt;jwt (json web token)&lt;/code&gt;. Passwords are hashed using &lt;code&gt;bcrypt&lt;/code&gt;. The API uses a MongoDB database.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Endpoints&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/users/&lt;/td&gt;
&lt;td&gt;POST  (public)&lt;/td&gt;
&lt;td&gt;register a user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/users/login&lt;/td&gt;
&lt;td&gt;POST (public)&lt;/td&gt;
&lt;td&gt;logs in a user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/users/me&lt;/td&gt;
&lt;td&gt;GET (private)&lt;/td&gt;
&lt;td&gt;gets the info of the logged in user&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;How to run&lt;/h2&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Clone the repository on your local computer&lt;/li&gt;
&lt;li&gt;Do &lt;code&gt;npm run install&lt;/code&gt; to install the required dependencies&lt;/li&gt;
&lt;li&gt;Set up your MongoDB collection&lt;/li&gt;
&lt;li&gt;Do &lt;code&gt;npm run dev&lt;/code&gt; to start the API&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Test the API with Postman&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DanielStai/jwt_auth" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h2&gt;
  
  
  How does JWT work
&lt;/h2&gt;

&lt;p&gt;Let's see how the JWT approach to authentication works. &lt;/p&gt;

&lt;p&gt;Say a user gives the correct credentials and logs in. The server will generate a token and send it back in the response. Subsequent requests will have the token in the authorization header. &lt;/p&gt;

&lt;p&gt;If the token is missing or expired, the user will not be able to continue accessing protected endpoints.&lt;/p&gt;

&lt;p&gt;The token consists of three parts i.e. the header, payload and the signature. You can &lt;a href="https://jwt.io/introduction" rel="noopener noreferrer"&gt;read more about what each of the parts of a jwt do here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is an overview of the JSON web authentication process&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh8ilyulokazborame911.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh8ilyulokazborame911.png" alt="jwt auth"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will also use the package &lt;code&gt;bcrypt&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project set up
&lt;/h2&gt;

&lt;p&gt;To show how to implement jwt authentication, we will use a simple API that implements login functionality for the users of a blog. Here are the endpoints of the API:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/users/&lt;/td&gt;
&lt;td&gt;POST  (public)&lt;/td&gt;
&lt;td&gt;register a user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/users/login&lt;/td&gt;
&lt;td&gt;POST (public)&lt;/td&gt;
&lt;td&gt;logs in a user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/users/me&lt;/td&gt;
&lt;td&gt;GET (private)&lt;/td&gt;
&lt;td&gt;gets the info of the logged in user&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As described above, we don't need to implement authentication to register a user and to login the user. We however need to ensure that when returning user info, we only do so for the current logged-in user. This will require some form of authentication.&lt;/p&gt;

&lt;p&gt;We will create a folder named &lt;code&gt;jwt_auth&lt;/code&gt;. This is where all our API code will reside.&lt;/p&gt;

&lt;p&gt;Lets create the &lt;code&gt;config&lt;/code&gt; (where our models reside), &lt;code&gt;routes&lt;/code&gt; (where we define the various routes in our API), &lt;code&gt;middleware&lt;/code&gt;&lt;br&gt;
 (where we will build error handling and authentication middleware) and &lt;code&gt;controllers&lt;/code&gt; (where we will define the callback functions fired when the routes in the &lt;code&gt;Routes&lt;/code&gt; directory are matched) directories. &lt;/p&gt;

&lt;p&gt;To set up the whole project you can refer to my articles on &lt;a href="https://dev.to/danielstai/how-to-build-an-express-rest-api-for-a-simple-blog-app-2m6k"&gt;how to build a REST API&lt;/a&gt; and &lt;a href="https://dev.to/danielstai/setting-up-mongodb-and-mongoose-for-your-project-5bob"&gt;how to set up MongoDB for your project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here's the structure of our project at the end:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92dip0x7u1zckrfdne8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92dip0x7u1zckrfdne8b.png" alt="auth structure"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementing &lt;code&gt;bcrypt&lt;/code&gt; and &lt;code&gt;jwt&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we've seen our project structure, we will implement our authentication in 3 steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encrypt password after logging in and generate token&lt;/li&gt;
&lt;li&gt;Create middleware to check if token exists and its validity &lt;/li&gt;
&lt;li&gt;Chain authentication middleware to the routes you need to protect&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can view the full project before and after adding authentication here (the version with authentication is on the branch 'authentication'):&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DanielStai" rel="noopener noreferrer"&gt;
        DanielStai
      &lt;/a&gt; / &lt;a href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;
        netninja-react-backend
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;REST API for a React FrontEnd&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;This is an express rest api for a simple blog app that enables one to manage a simple blog.&lt;/p&gt;
&lt;p&gt;You can view the client app &lt;a href="https://www.youtube.com/watch?v=j942wKiXFu8" rel="nofollow noopener noreferrer"&gt;here&lt;/a&gt; .&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Endpoints&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches all blogs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;post a blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;delete a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;This project has been discussed over several blog posts hence we will only concern ourselves with implementing some authentication&lt;/p&gt;

&lt;p&gt;Let's view our &lt;code&gt;jwt_auth/controllers/userController.js&lt;/code&gt; file and see how we hash our passwords and generate our tokens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;asyncHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-async-handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../models/userModel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bcryptjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;registerUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please fill in all fields&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userExists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userExists&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User already exists&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;genSalt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hashedPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;hashedPassword&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nf"&gt;generateToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid user data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;loginUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nf"&gt;generateToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid credentials&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getThisUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generateToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;expiresIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;30d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;loginUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;getThisUser&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Encrypting the password with &lt;code&gt;bcrypt&lt;/code&gt; is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;genSalt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hashedPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We first create a salt with bcrypt's &lt;code&gt;genSalt&lt;/code&gt; method and use this salt and the inputted password by the user to generate the &lt;code&gt;hashedPassword&lt;/code&gt;. This is the password that will be stored in the database and not the user's real password. This is a security measure to prevent malicious parties from accessing passwords stored in a database.&lt;/p&gt;

&lt;p&gt;One would then ask, how can we confirm a user's password if we don't have the raw password stored in the database? In our case, we will use bcrypt's handy &lt;code&gt;compare()&lt;/code&gt; method. Let's see it in action in our &lt;code&gt;userController.js&lt;/code&gt; file when we to log in a user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;loginUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;bcrypt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nf"&gt;generateToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid credentials&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above is the &lt;code&gt;loginUser&lt;/code&gt; function which is called when the route /&lt;code&gt;api/users/login&lt;/code&gt; is matched. We first obtain the email and password from destructuring the request body. We then use the Mongoose method &lt;code&gt;findOne()&lt;/code&gt; to find the exact user with the specified email address. To verify the user, we compare the inputted password and the password in the database using &lt;code&gt;bcrypt.compare()&lt;/code&gt;. It takes the keyed in password and the password in the database as arguments and if the passwords match it returns true.&lt;/p&gt;

&lt;p&gt;If the user exists in the database and the password is verified, &lt;code&gt;loginUser&lt;/code&gt; returns the user's details including a token that we can use to access some restricted content.&lt;/p&gt;

&lt;p&gt;Let's see how we generate the token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generateToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;expiresIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;30d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;generateToken&lt;/code&gt; function takes in the user's id as an argument. We will then use jwt's &lt;code&gt;sign()&lt;/code&gt; method to create and return the token. &lt;code&gt;sign()&lt;/code&gt; takes in 3 arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;id: The id of the user logging in&lt;/li&gt;
&lt;li&gt;secret: This is a private signing key that we store in our dotenv file. It confirms the origin of the token&lt;/li&gt;
&lt;li&gt;expiring time: We can add an optional argument to specify when the token expires&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will now create our authentication middleware which will make sure that protected routes are accompanied with a valid token. This is how we implement authentication with jwt in express apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Authentication middleware
&lt;/h2&gt;

&lt;p&gt;Some routes need to be accessed privately (as specified in the API definition) and the way we do so is by chaining some middleware to the route definition.&lt;/p&gt;

&lt;p&gt;We will create our authentication middleware in our &lt;code&gt;middleware&lt;/code&gt; directory in the file &lt;code&gt;authMiddleware.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's take a look at our auth middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsonwebtoken&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;asyncHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-async-handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../models/userModel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;protect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bearer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//Get token from header&lt;/span&gt;
            &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

            &lt;span class="c1"&gt;//Verify token&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;JWT_SECRET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Not authorized&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;protect&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above might seem like alot but it's actually quite simple. We are simply using javascript methods to get a token from the request header and verifying this token. We also set the error message if token doesn't exist or isnt valid ('Not authorized'). &lt;/p&gt;

&lt;p&gt;At the end we export our authentication function as &lt;code&gt;protect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How can we the utilize this &lt;code&gt;protect&lt;/code&gt; function to protect some routes? &lt;/p&gt;

&lt;p&gt;Let's have a look at our routes file now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loginUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getThisUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../controllers/userControllers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;protect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../middleware/authMiddleware&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loginUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/me&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;protect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getThisUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only route we need to protect is the &lt;code&gt;api/users/me&lt;/code&gt; route&lt;br&gt;
and we do so by including &lt;code&gt;protect&lt;/code&gt; in the route definition as seen above. It's that easy!&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing our authentication
&lt;/h2&gt;

&lt;p&gt;Let's do a recap of what we have so far. We have defined several routes for a REST API that implements some login functionality. For one specific route that returns a specific user's info, we need to do some form of authentication to prevent unwanted access. To do so we implement some middleware and chain it to our route definition.&lt;/p&gt;

&lt;p&gt;Now that we have added authentication to our project, lets test if it works&lt;/p&gt;

&lt;p&gt;We will start by creating a new user called Daniel with the email address of &lt;a href="mailto:daniel@yahoo.com"&gt;daniel@yahoo.com&lt;/a&gt; and a password of 1234:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ybn290oo1ue7qa2w4he.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ybn290oo1ue7qa2w4he.png" alt="create user"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that a token is generated when we create a user.&lt;/p&gt;

&lt;p&gt;Now let's login using the user details we have created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F22omwzwy1bzzipmjssa5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F22omwzwy1bzzipmjssa5.png" alt="logon user"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To confirm our error handling works, lets use a wrong password and see the response generated:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4jqhjcit4xpqg8p6bzh5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4jqhjcit4xpqg8p6bzh5.png" alt="invalid credentials"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We get the error message of "Invalid credentials" as desired&lt;/p&gt;

&lt;p&gt;Let's now try accessing &lt;code&gt;GET api/users/me&lt;/code&gt; without using our token:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ichy9hbo6en51srm9zb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ichy9hbo6en51srm9zb.png" alt="not authorized"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We get an error message that we are not authorized to access that particular route&lt;/p&gt;

&lt;p&gt;Let's try accessing the same route but this time pass in the correct token:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F740yw3dd66tmj5md3jnc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F740yw3dd66tmj5md3jnc.png" alt="authorized"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we have no error messages and we get the content we want back from a protected route. Success!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we implemented authentication using jwt. In the next article we will deploy our express app to the internet using Railway. You can read that article here.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Setting up MongoDB and Mongoose for your project</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Wed, 07 Dec 2022 05:29:28 +0000</pubDate>
      <link>https://dev.to/danielstai/setting-up-mongodb-and-mongoose-for-your-project-5bob</link>
      <guid>https://dev.to/danielstai/setting-up-mongodb-and-mongoose-for-your-project-5bob</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we will look at how to set up our project from my &lt;a href="https://dev.to/danielstai/how-to-build-an-express-rest-api-for-a-simple-blog-app-2m6k"&gt;previous tutorial&lt;/a&gt; to use MongoDB.&lt;/p&gt;

&lt;p&gt;In the previous article, we built a REST API that served data to our blog application. We build out endpoints to create, read, update and delete data. This data however is stored in an array in our &lt;code&gt;blogController.js&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;This works when testing the application but all the changes made are temporary. If you create a new blog entry, this blog entry will remain visible but disappears when you restart the server.&lt;/p&gt;

&lt;p&gt;We will now introduce a database to our project so as to persist our data. There are many different choices for a database depending on your applications needs.&lt;/p&gt;

&lt;p&gt;For our application, we will use MongoDB with Mongoose.&lt;/p&gt;

&lt;p&gt;MongoDB is a NoSQL document database used to build highly available and scalable applications.&lt;/p&gt;

&lt;p&gt;Mongoose is an ODM (Object Data Modelling) library used to make it easier to connect to MongoDB by providing functions to manipulate the documents in collections.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DanielStai" rel="noopener noreferrer"&gt;
        DanielStai
      &lt;/a&gt; / &lt;a href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;
        netninja-react-backend
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;REST API for a React FrontEnd&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;This is an express rest api for a simple blog app that enables one to manage a simple blog.&lt;/p&gt;
&lt;p&gt;You can view the client app &lt;a href="https://www.youtube.com/watch?v=j942wKiXFu8" rel="nofollow noopener noreferrer"&gt;here&lt;/a&gt; .&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Endpoints&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches all blogs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;post a blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;delete a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;We have already built out the skeleton of this project in my previous article. As a recap, here's what we have so far.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprkkjo6xoh9hzd0qk4t3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprkkjo6xoh9hzd0qk4t3.png" alt="Project structure" width="173" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We created a directory called &lt;code&gt;server&lt;/code&gt;. In this &lt;code&gt;server&lt;/code&gt; directory we have our &lt;code&gt;server.js&lt;/code&gt; file which is our entry point to our API. We also have &lt;code&gt;routes&lt;/code&gt; and &lt;code&gt;controllers&lt;/code&gt; directories which hold the routes and the controllers (callback functions that are fired when these routes are matched) respectively. &lt;/p&gt;

&lt;p&gt;That's the structure we have so far. Next we will do the database configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting to MongoDB
&lt;/h2&gt;

&lt;p&gt;As described before, MongoDB is a document based database that allows us to build highly scalable applications. You can install MongoDB server locally but for our case, we will use MongoDB Atlas.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/basics/mongodb-atlas-tutorial" rel="noopener noreferrer"&gt;MongoDB Atlas&lt;/a&gt; is a fully-managed cloud database that handles all the complexity of deploying, managing, and healing your deployments on the cloud service provider of your choice (AWS , Azure, and GCP).&lt;/p&gt;

&lt;p&gt;To get started, lets navigate to MongoDB homepage, create an account and login. &lt;/p&gt;

&lt;p&gt;Click on "Create new organization" and give your organization a name. Let's call our organization &lt;code&gt;tutorials&lt;/code&gt; and choose MongoDB Atlas as our preferred Cloud Service. Click on next and confirm organization creation on the next page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu500e35mg2vpcwwploo7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu500e35mg2vpcwwploo7.png" alt="create organization" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the organization is created, we need to initialize a new project. Once you click on "create organization" you will be redirected to a projects page which is empty so far. Let's create a new project. Click on new &lt;code&gt;project&lt;/code&gt; as below and name it &lt;code&gt;dojo-blog-app&lt;/code&gt;. This is in line with the name of the blog app we are creating this database for. Confirm project creation on the subsequent pages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fefoly5po6vey6ppc2yrw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fefoly5po6vey6ppc2yrw.png" alt="creating new project" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With our project up and running, we now need to create a database. Indeed after confirming project creation, we are now redirected to a "Create database" page. Let's click on "Build Database" and create a database for our project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwvtgazuk6m8mg5mn6lc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwvtgazuk6m8mg5mn6lc5.png" alt="create database" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are now redirected to choose a hosting plan. We, for the purposes of this tutorial, will use the free shared plan.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jgl5s5wlkt1b6rss7mb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jgl5s5wlkt1b6rss7mb.png" alt="create clusters" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will now be required to do some configuration for our shared cluster. As a tip, most of the recommended settings here can do. The only change we will make is naming our cluster something memorable than &lt;code&gt;cluster0&lt;/code&gt;. Lets call our cluster &lt;code&gt;dojo-cluster&lt;/code&gt;. Click on "Create".&lt;/p&gt;

&lt;p&gt;Next up is a few more settings to configure the security of our database and clusters. You'll be asked how you would like to authenticate your connection (username and password) and where you would like to connect from (local environment).&lt;/p&gt;

&lt;p&gt;Create a username and password.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ghn8elj9pkz4vnoxed4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ghn8elj9pkz4vnoxed4.png" alt="username and password" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select "My local environment" as where you want to connect from and also add your current IP address to the list of whitelisted IP addresses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vizyg6mlfxbw6ynobw4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vizyg6mlfxbw6ynobw4.png" alt="where to connect from" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on finish and close and we are now done setting up our database. Under our created project, we can now see the cluster we have created (&lt;code&gt;dojo-cluster&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb71v559rbhwe2g3rscku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb71v559rbhwe2g3rscku.png" alt="cluster confirmation" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the clusters up and running, let's now focus on connecting our database to the rest of our REST API (no pun intended). Navigate to your cluster (like in the above screenshot) and click on "Connect".&lt;/p&gt;

&lt;p&gt;There are several options to connect. Primarily, we want to get a connection string that will be domiciled in our dotenv file that will enable us to connect to our database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F51danb5fv1hl8eotj68e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F51danb5fv1hl8eotj68e.png" alt="Conection options" width="800" height="680"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's click on the second option which is "Connect your application to your cluster using MongoDB's native drivers". Here we can now get a string that we will copy and paste in our dotenv file in our application code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhx5l49aj1ui15pia4z54.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhx5l49aj1ui15pia4z54.png" alt="connection string" width="800" height="680"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the connection string and save it somewhere as we will need it up next as we refactor our existing code to use our database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting MongoDB to our project
&lt;/h2&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/danielstai/how-to-build-an-express-rest-api-for-a-simple-blog-app-2m6k"&gt;previous tutorial/article&lt;/a&gt;, we saw how we can set up a REST API for a simple blog app to give it some CRUD functionality without having a functional database. &lt;/p&gt;

&lt;p&gt;We are now implementing the database in this phase of the project. &lt;/p&gt;

&lt;p&gt;In the previous section, we created a MongoDB Atlas account and set up a cluster. In this section, we will now connect our app to the cluster.&lt;/p&gt;

&lt;p&gt;Create a new directory inside the &lt;code&gt;server&lt;/code&gt; directory and lets name it &lt;code&gt;config&lt;/code&gt;. Inside &lt;code&gt;config&lt;/code&gt; lets create a file called &lt;code&gt;db.js&lt;/code&gt; and this is where we will write all our database connection code. &lt;/p&gt;

&lt;p&gt;Copy and paste the code below into your &lt;code&gt;db.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectDB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGO_URI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Database connected &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cyan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;underline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;connectDB&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We begin by importing &lt;code&gt;mongoose&lt;/code&gt; into our project (do &lt;code&gt;npm install mongoose&lt;/code&gt; if not installed yet). We will then create a function called &lt;code&gt;connectDB&lt;/code&gt;. This is the function we will call when we want to connect to our database in our main &lt;code&gt;server.js&lt;/code&gt; file. This is an asynchronous function since we are now working with a database. &lt;/p&gt;

&lt;p&gt;In &lt;code&gt;connectDB&lt;/code&gt;, we will use the Mongoose method &lt;code&gt;mongoose.connect&lt;/code&gt; to connect to our cluster. This method takes in a connection string as an argument. &lt;/p&gt;

&lt;p&gt;Remember the connection string we copied and stored somewhere safe? We will now create an environment variable &lt;code&gt;MONGO_URI&lt;/code&gt; and give it the value of the stored string as seen below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB: Remember to replace &lt;code&gt;password&lt;/code&gt; with the actual password to the cluster that you created.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvpggfl3goeglk83s6ev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsvpggfl3goeglk83s6ev.png" alt="dotenv" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We then log a statement to our console to show that our connection is successful. The &lt;code&gt;cyan.underline&lt;/code&gt; appended to the &lt;code&gt;console.log()&lt;/code&gt; statement changes the color of the logged statement to cyan and is a feature of the &lt;code&gt;colors&lt;/code&gt; package (do &lt;code&gt;npm install colors&lt;/code&gt; if not installed yet). &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;catch&lt;/code&gt; part of our &lt;code&gt;try catch&lt;/code&gt; statement simply logs an error if any and exits the process. Lastly, we do a &lt;code&gt;module.exports&lt;/code&gt; statement so as to export the function &lt;code&gt;connectDB&lt;/code&gt; to be used elsewhere, in this case the &lt;code&gt;server.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating models
&lt;/h2&gt;

&lt;p&gt;Now that we have the connection to the database up and running, let us create models for our database. We will first define the structure of our database using a schema. &lt;/p&gt;

&lt;p&gt;Generally speaking, a schema is a structured framework or an outline. A database schema therefore defines how data is organized within a database including logical constraints such as, collection names, fields, data types, and the relationships between these entities.&lt;/p&gt;

&lt;p&gt;Create a new directory named &lt;code&gt;models&lt;/code&gt; where we will store our various data models. For our blog app, create a new file in the models directory called &lt;code&gt;blogModel.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;At the moment we are only concerned with a simple blog app but later we may need to add more functionality such as users etc and thus we may need to create new models. These models will be created in separate files eg &lt;code&gt;userModel.js&lt;/code&gt;. Keeping each model separate is a good practice that ensures modularization of our code.&lt;/p&gt;

&lt;p&gt;Let's have a look at &lt;code&gt;blogModel.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blogSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please add a title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please add a story&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please add an author name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;},{&lt;/span&gt;
    &lt;span class="na"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;blogSchema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As usual, we start by importing Mongoose. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.&lt;/p&gt;

&lt;p&gt;To create our database schema, we will use the very handy &lt;code&gt;Mongoose.schema()&lt;/code&gt; method to define &lt;code&gt;blogSchema&lt;/code&gt;. Our blog entries have 3 fields i.e. title, author, and the blog body. MongoDB adds a unique ID field for us so we wont add that in our schema definition. &lt;/p&gt;

&lt;p&gt;Read more about mongoose and schemas &lt;a href="https://mongoosejs.com/docs/guide.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After defining our fields and the data types in the schema, we can add another argument to our &lt;code&gt;Mongoose.schema()&lt;/code&gt; method that adds timestamps for when we create or update posts.&lt;/p&gt;

&lt;p&gt;As always, don't forget to export the model so that we can use it in our controller functions. We will use the &lt;code&gt;mongoose.model()&lt;/code&gt; method. The &lt;code&gt;mongoose.model()&lt;/code&gt; function of the mongoose module is used to &lt;a href="https://www.geeksforgeeks.org/mongoose-mongoose-model-function/" rel="noopener noreferrer"&gt;create a collection of a particular database of MongoDB&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating controllers
&lt;/h2&gt;

&lt;p&gt;Now that we have a single function we can fire up to connect to the database, let us re-purpose our controller functions to read, write or delete data from our database and not the array we were using before. &lt;/p&gt;

&lt;p&gt;Let's see how our &lt;code&gt;controllers/blogControllers.js&lt;/code&gt; file will now look like and then we will discuss the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;asyncHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-async-handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../models/blogModel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;// @desc Get all blogs&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="c1"&gt;// @desc Get single blog&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/{id}&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="c1"&gt;// @desc Post a blog&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/{id}&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;postBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please fill all the required fields&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="c1"&gt;// @desc Delete a blog&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/{id}&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deleteBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blog deleted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;getBlogs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;getBlog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;postBlog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;deleteBlog&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have 4 controller functions which are fired up when specific routes are matched. Our API has the following endpoints:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches all blogs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;posts a new blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;deletes a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In &lt;code&gt;blogController.js&lt;/code&gt;, we start by importing &lt;code&gt;asyncHandler&lt;/code&gt;. &lt;code&gt;asyncHandler&lt;/code&gt; wraps async functions and &lt;a href="https://stackoverflow.com/questions/56973265/what-does-express-async-handler-do" rel="noopener noreferrer"&gt;saves you writing your own try/catch for async/await and passes errors on to next&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will also import the &lt;code&gt;Blog&lt;/code&gt; model we created in our models directory&lt;/p&gt;

&lt;p&gt;To get all blogs, we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;Blog&lt;/code&gt; model, we will use the &lt;code&gt;find()&lt;/code&gt; method provided by mongoose. &lt;code&gt;find()&lt;/code&gt; returns all records found. Sometimes you may need to return a particular record by id etc and there are methods to do that. Read more about mongoose queries &lt;a href="https://mongoosejs.com/docs/queries.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To get a specific blog, we have the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To return a specific blog post, we make use of the mongoose query &lt;code&gt;findById()&lt;/code&gt;. We then check whether the blog with the requested id exists. We get the id from the request using &lt;code&gt;req.params.id&lt;/code&gt;. If not we show an error message of 'Blog not found'. Otherwise we return the requested post.&lt;/p&gt;

&lt;p&gt;To post a blog we have the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;postBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please fill all the required fields&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use an &lt;code&gt;if/else&lt;/code&gt; block to take care of empty entries. After checking that all fields have been filled, we then use the &lt;code&gt;Model.create()&lt;/code&gt; method to create a new document in our collection. If successful, we return the created blog with a status code of 200. &lt;/p&gt;

&lt;p&gt;To delete a blog, we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deleteBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asyncHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blog deleted&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We target the post we want to delete using &lt;code&gt;findById()&lt;/code&gt;. Once located, we use mongoose's &lt;code&gt;remove()&lt;/code&gt; method to remove it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Custom error handling middleware
&lt;/h2&gt;

&lt;p&gt;One advantage of using Express.js is that it is unopinionated and allows us the flexibility to tackle challenges using custom solutions. &lt;/p&gt;

&lt;p&gt;This is in contrast to opinionated frameworks (e.g. Django) which have set rules to tackle most problems. &lt;/p&gt;

&lt;p&gt;To illustrate the unopinionated nature of express, we will build a custom error handler and chain it to all our requests as middleware.&lt;/p&gt;

&lt;p&gt;In our root directory of &lt;code&gt;server&lt;/code&gt;, lets create a &lt;code&gt;middleware&lt;/code&gt; directory and inside it create the file &lt;code&gt;errorHandler.js&lt;/code&gt;. In this file, we will implement some custom error handling. This will include the option to have verbose or abbreviated error messages depending on whether the app is in development or production mode.&lt;/p&gt;

&lt;p&gt;Let's have a look at &lt;code&gt;errorHandler.js&lt;/code&gt; and discuss it below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errorHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;errorHandler&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will define an error handler function which takes in 4 arguments (i.e. err, res, req and next). &lt;/p&gt;

&lt;p&gt;In the callback, we check whether the response has a &lt;code&gt;statusCode&lt;/code&gt;. If not we return a generic &lt;code&gt;500&lt;/code&gt; (Internal server error). We then want to return  an error message (from err.message) and a full description of the error (from err.stack) when &lt;code&gt;process.env.NODE_ENV === 'development'&lt;/code&gt;. If we are in production, we want to see a more abbreviated description of the error (in this case &lt;code&gt;null&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the API
&lt;/h2&gt;

&lt;p&gt;Congratulations. We have built a fully functional REST API complete with a supporting MongoDB database. In my previous article, we tested our API using Postman and all the endpoints worked as they should.&lt;/p&gt;

&lt;p&gt;We shall follow a similar approach and retest our endpoints seeing as we have made significant changes to our API.&lt;/p&gt;

&lt;p&gt;Lets fire up Postman and start by testing &lt;code&gt;GET /api/blogs/&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fillya5z77nm4o24eg8fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fillya5z77nm4o24eg8fu.png" alt="test get all blogs" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As expected, we retrieve all blogs we have at the moment i.e. 2 blog posts.&lt;/p&gt;

&lt;p&gt;Next we will use one of the blog id's we get from requesting all blogs above to test our endpoint that retrieves a single blog i.e. &lt;code&gt;GET /api/blogs/{id}&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhy82kk8cmogs2mexjeqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhy82kk8cmogs2mexjeqw.png" alt="get single post" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila. Both retrieving all blogs and retrieving a single blog work as expected.&lt;/p&gt;

&lt;p&gt;I will leave the two remaining methods i.e. POST and DELETE for you the reader to test them out tell me what you get by leaving a comment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's the end of this particular article. We have come a long way from building dummy express apps to now building a fully functional rest api. &lt;/p&gt;

&lt;p&gt;To build further on this, we will add some type of authentication to our app in the next article.&lt;/p&gt;

&lt;p&gt;Till then, adios.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up MongoDB
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Connecting MongoDB to our project
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Changing code to fit MongoDB (creating schemas etc)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Testing with Postman
&lt;/h2&gt;

</description>
      <category>discuss</category>
      <category>career</category>
    </item>
    <item>
      <title>How to build an Express REST API for a simple blog app</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Wed, 23 Nov 2022 07:06:44 +0000</pubDate>
      <link>https://dev.to/danielstai/how-to-build-an-express-rest-api-for-a-simple-blog-app-2m6k</link>
      <guid>https://dev.to/danielstai/how-to-build-an-express-rest-api-for-a-simple-blog-app-2m6k</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article I will build a simple express server for the React tutorial by Net Ninja on YouTube. Here's a link to &lt;a href="https://www.youtube.com/watch?v=j942wKiXFu8" rel="noopener noreferrer"&gt;that tutorial&lt;/a&gt;. Here's how the front-end client we are building the API for looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc6xuyo28komng63dapxj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc6xuyo28komng63dapxj.png" alt="blog app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The front-end app enables one to view blogs, click on a blog to view a single blog, delete a single blog and post a new blog. The YouTube tutorial uses json server which is an important tool for front end developers to simulate calls to a REST API. &lt;/p&gt;

&lt;p&gt;We will use this as the motivation to build and test a fully functional REST API with Express.js to serve this blog app.&lt;/p&gt;

&lt;p&gt;The code for this tutorial can be found at the repo below.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DanielStai" rel="noopener noreferrer"&gt;
        DanielStai
      &lt;/a&gt; / &lt;a href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;
        netninja-react-backend
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;REST API for a React FrontEnd&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;This is an express rest api for a simple blog app that enables one to manage a simple blog.&lt;/p&gt;
&lt;p&gt;You can view the client app &lt;a href="https://www.youtube.com/watch?v=j942wKiXFu8" rel="nofollow noopener noreferrer"&gt;here&lt;/a&gt; .&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Endpoints&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches all blogs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;post a blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;delete a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h2&gt;
  
  
  Endpoints
&lt;/h2&gt;

&lt;p&gt;As described above, we are building an express api that will serve data to a simple front end blog app. &lt;/p&gt;

&lt;p&gt;Users will be able to view all posts, view a single post, post a new blog and finally delete a blog post. Our task will therefore to build the endpoints that will actualize this functionality.&lt;/p&gt;

&lt;p&gt;Let's discuss the endpoints we will build out:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;URL&lt;/th&gt;
&lt;th&gt;METHOD&lt;/th&gt;
&lt;th&gt;DESCRIPTION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches all blogs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;fetches a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;posts a new blog&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;api/blogs/{id}&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;deletes a single blog&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Project setup
&lt;/h2&gt;

&lt;p&gt;In my previous articles, we have built out all our API functionality in our &lt;code&gt;server.js&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;For this tutorial, we will adopt some best practices and separate our routes and logic into separate folders. More on that later&lt;/p&gt;

&lt;p&gt;Let's create a folder to host our project. I'll call mine server. Let's &lt;code&gt;cd&lt;/code&gt; into the server folder. In the terminal, we will run&lt;code&gt;npm init -y&lt;/code&gt; to build a skeleton node project. &lt;/p&gt;

&lt;p&gt;Lets create the js file that will hold all our API code and call it &lt;code&gt;server.js&lt;/code&gt;. This will be the 'entry point' to our API.&lt;/p&gt;

&lt;p&gt;While still in your project directory, lets run &lt;code&gt;npm install express cors body-parser&lt;/code&gt; in the terminal. &lt;/p&gt;

&lt;p&gt;This will install &lt;code&gt;express&lt;/code&gt;, &lt;code&gt;cors&lt;/code&gt; and &lt;code&gt;body-parser&lt;/code&gt;, packages that will make our API development process smoother. &lt;/p&gt;

&lt;p&gt;To read further on the same you can read &lt;a href="https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i"&gt;an article I wrote&lt;/a&gt; about setting up a simple express server that explains how the above packages are used. &lt;/p&gt;

&lt;p&gt;While we are at it, lets also install nodemon by running &lt;code&gt;npm install -D nodemon&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Nodemon refreshes our server for us. Otherwise we would be forced to stop and start our server every time we make a change.&lt;/p&gt;

&lt;p&gt;The -D flag ensures nodemon is installed as a dev dependency&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;package.json&lt;/code&gt;, we will edit &lt;code&gt;"main":"main.js"&lt;/code&gt; to &lt;code&gt;"main":"server.js"&lt;/code&gt;. Last but not least, under scripts we will add a new script to run nodemon as follows &lt;code&gt;"dev":"nodemon server.js"&lt;/code&gt;. Lets start writing our api code in server.js&lt;/p&gt;

&lt;p&gt;As stated earlier, we will have our routes and logic in different folders. Lets create a 2 new directories in our server directory and name them &lt;code&gt;routes&lt;/code&gt; and &lt;code&gt;controllers&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Routes
&lt;/h2&gt;

&lt;p&gt;We will start by importing &lt;code&gt;express&lt;/code&gt;, &lt;code&gt;cors&lt;/code&gt; and &lt;code&gt;bodyparser&lt;/code&gt; and mounting all the required middleware. Here's what we have so far in &lt;code&gt;server.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;extended&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a simple API, we could in &lt;code&gt;server.js&lt;/code&gt; define all the endpoints and the functions that are fired when a request is made to that endpoint, but for this tutorial, we will take a different approach.&lt;/p&gt;

&lt;p&gt;Let's assume your API serves a huge front end application and many different endpoints for different features are needed. It would be very messy and difficult to maintain if all this functionality was in the &lt;code&gt;server.js&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;It's for this reason that we will create a &lt;code&gt;blogRoutes.js&lt;/code&gt; file in our &lt;code&gt;routes&lt;/code&gt; folder for all the routes in our blogs feature. &lt;/p&gt;

&lt;p&gt;If, for instance, we added users to our app and needed to provide authentication etc, we would create a file for those routes in our &lt;code&gt;routes&lt;/code&gt; folder and name it, say, &lt;code&gt;userRoutes.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will be our file structure:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4z0gyey5yghbp3d80ry8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4z0gyey5yghbp3d80ry8.png" alt="structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's add the following line in &lt;code&gt;server.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/blogs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./routes/blogRoutes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This defines our base route as &lt;code&gt;/api/blogs&lt;/code&gt; and also tells us that the various routes for our API are set in our &lt;code&gt;/routes/blogRoutes&lt;/code&gt; path.&lt;/p&gt;

&lt;p&gt;Now let's see what we have in blogRoutes.js file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getBlogs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;postBlog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getBlog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteBlog&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../controllers/blogControllers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getBlogs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;postBlog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getBlog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deleteBlog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will import &lt;code&gt;express&lt;/code&gt; and the &lt;code&gt;router&lt;/code&gt; method to use in our &lt;code&gt;blogRoutes&lt;/code&gt; file. We will also import some functions from &lt;code&gt;../controllers/blogControllers&lt;/code&gt; (which we haven't built out yet)&lt;/p&gt;

&lt;p&gt;From our &lt;code&gt;server.js&lt;/code&gt; file, we know that our base API route is &lt;code&gt;/api/blogs&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Here in &lt;code&gt;blogRoutes.js&lt;/code&gt;, we will define what happens when we access the endpoints &lt;code&gt;api/blogs/&lt;/code&gt; and &lt;code&gt;/api/blogs/{id}&lt;/code&gt; with either GET, POST or DELETE requests.&lt;/p&gt;

&lt;p&gt;Let's take a look at &lt;code&gt;router.get('/', getBlogs)&lt;/code&gt;. When the endpoint &lt;code&gt;/api/blogs/&lt;/code&gt; is accessed, it will fire a function &lt;code&gt;getBlogs&lt;/code&gt; which retrieves all blog posts.&lt;/p&gt;

&lt;p&gt;We can further clean up our code by condensing the four lines that define our routes as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getBlogs&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postBlog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getBlog&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deleteBlog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Up next we will have a look at &lt;code&gt;blogControllers.js&lt;/code&gt; and see what the functions &lt;code&gt;getBlogs&lt;/code&gt;, &lt;code&gt;getBlog&lt;/code&gt;, &lt;code&gt;postBlog&lt;/code&gt; and &lt;code&gt;deleteBlog&lt;/code&gt; do. &lt;/p&gt;

&lt;p&gt;Don't forget to &lt;code&gt;module.exports = router&lt;/code&gt; as this line enables us to export the routes we have created to be used in our &lt;code&gt;server.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining controllers
&lt;/h2&gt;

&lt;p&gt;Let's recap what we have so far. &lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;server.js&lt;/code&gt; file, we added this line of code (&lt;code&gt;app.use('/api/blogs', require('./routes/blogRoutes'))&lt;/code&gt;) that instructs our API to look in the folder &lt;code&gt;./routes/blogRoutes&lt;/code&gt; for the specific implementation of our endpoints.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;blogRoutes.js&lt;/code&gt;, we defined what happens when we hit various endpoints with various request methods. &lt;/p&gt;

&lt;p&gt;We specifically looked at what happens when we get a &lt;code&gt;GET&lt;/code&gt; request to &lt;code&gt;/api/blogs/&lt;/code&gt;. We saw that a function called &lt;code&gt;getBlogs&lt;/code&gt; (that has been imported from &lt;code&gt;../controllers/blogControllers&lt;/code&gt;) is fired and it returns all the blog posts in our data storage. &lt;/p&gt;

&lt;p&gt;This is the same for all the other routes in &lt;code&gt;blogRoutes.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Recap done! Now let's have a look at the four functions we imported into blogRoutes.js namely &lt;code&gt;getBlogs&lt;/code&gt;, &lt;code&gt;getBlog&lt;/code&gt;, &lt;code&gt;deleteBlog&lt;/code&gt; and &lt;code&gt;postBlog&lt;/code&gt;. These are what we'll call our controller functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My First Blog&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Insert some text here&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mario&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My Second Blog&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Insert some more text here&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yoshi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// @desc Get all blogs&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// @desc Get single blog&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/{id}&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; 

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// @desc Post a blog&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/{id}&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;postBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;
    &lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New post has been added&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// @desc Delete a blog&lt;/span&gt;
&lt;span class="c1"&gt;// @route /api/blogs/{id}&lt;/span&gt;
&lt;span class="c1"&gt;// @access public&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deleteBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

    &lt;span class="nx"&gt;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Blog has been deleted&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;getBlogs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;getBlog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;postBlog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;deleteBlog&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pasted above is the entirety of our &lt;code&gt;blogControllers.js&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;Let us discuss the code. &lt;/p&gt;

&lt;p&gt;We start with creating an array called blogs which acts as our data storage for our blogs for now. This means our data is temporary. In a later tutorial we will implement a database which will help make our data persist. &lt;/p&gt;

&lt;p&gt;We then define four functions i.e. &lt;code&gt;getBlogs&lt;/code&gt;, &lt;code&gt;getBlog&lt;/code&gt;, &lt;code&gt;postBlog&lt;/code&gt;, &lt;code&gt;deleteBlog&lt;/code&gt;. These functions hold the logic that makes our endpoints do exactly what we want them to do. &lt;/p&gt;

&lt;p&gt;After defining these functions, we export them to &lt;code&gt;blogRoutes.js&lt;/code&gt; file where we will combine the path, the request method and logic to return or receive the data we want&lt;/p&gt;

&lt;p&gt;Let's take a look at &lt;code&gt;getBlog&lt;/code&gt; for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getBlog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; 

    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function returns a single blog post which matches the &lt;code&gt;id&lt;/code&gt; in the request. &lt;/p&gt;

&lt;p&gt;As specified in &lt;code&gt;blogRoutes.js&lt;/code&gt;, the function is fired when we get a &lt;code&gt;GET&lt;/code&gt; request to the route defined as &lt;code&gt;/blogs/:id&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The : indicates that the id variable can take any value. We then use &lt;code&gt;req.params.id&lt;/code&gt; to retrieve the id from the incoming request and we store it in a variable &lt;code&gt;id&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We will then check our &lt;code&gt;blogs&lt;/code&gt; array for &lt;code&gt;blog.id === id&lt;/code&gt;. Once we have it, we will send a response back in the form of a json object. If it doesn't exist, we will send back an error message indicating the same.&lt;/p&gt;

&lt;p&gt;Have a look at the rest of the functions and try and figure out what each does. &lt;/p&gt;

&lt;p&gt;Each controller function has a comment with a description, route and who can access that route.&lt;/p&gt;

&lt;p&gt;Do not forget to export the functions created (using &lt;code&gt;module.exports&lt;/code&gt;) so that we can be able to use them elsewhere in our app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the API
&lt;/h2&gt;

&lt;p&gt;We now have all the 4 endpoints we set out to build from the start. &lt;/p&gt;

&lt;p&gt;How can we tell if they work as we want them to?&lt;/p&gt;

&lt;p&gt;One way is to integrate them with our frontend app (which has already been built out in &lt;a href="https://www.youtube.com/watch?v=j942wKiXFu8" rel="noopener noreferrer"&gt;this tutorial&lt;/a&gt;)and see if we get the desired results.&lt;/p&gt;

&lt;p&gt;However, a better way to test your APIs as a backend developer is using Postman to test your endpoints. &lt;/p&gt;

&lt;p&gt;Let's test two of our endpoints namely &lt;code&gt;GET api/blogs/&lt;/code&gt; and &lt;code&gt;GET api/blogs/{id}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First you need to start your server if it wasn't running already with &lt;code&gt;npm run dev&lt;/code&gt;. If you get errors in your terminal, you can always go through the tutorial again or check out the code in &lt;a href="https://github.com/DanielStai/netninja-react-backend" rel="noopener noreferrer"&gt;its GitHub repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Fire up Postman in your computer. If not yet installed, you can &lt;a href="https://www.postman.com/downloads/" rel="noopener noreferrer"&gt;find instructions to do so here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Open a new tab, choose &lt;code&gt;GET&lt;/code&gt; as the method you want to use and in the query box alongside it, type in your API link in this case &lt;code&gt;http://localhost:4000/api/blogs/&lt;/code&gt;. Click on send:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7au2bv9y89r35z585sp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7au2bv9y89r35z585sp.png" alt="test all blogs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both our objects in the blogs array are returned. Success!&lt;/p&gt;

&lt;p&gt;Let's try returning the blog with the id of 1:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqtaj189d3e94mdqi5oa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmqtaj189d3e94mdqi5oa.png" alt="test_one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, success.&lt;/p&gt;

&lt;p&gt;What happens if you try to access a blog that doesnt exist in our &lt;code&gt;blogs&lt;/code&gt; array? Lets try fetching a blog with an id of 3:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspn7bnqbiiz3wzmc3hua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspn7bnqbiiz3wzmc3hua.png" alt="not found"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We don't get a blog object back. As expected, we get a message back that the blog has not been found.&lt;/p&gt;

&lt;p&gt;What about our post and delete endpoints? If you've followed along so far, please test them out and let me know in the comments section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's the end of our tutorial on how to build a REST API for a simple client. To take this to the next level, we will add a database so as to make our data persist in subsequent articles.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>How to connect a React frontend to an Express server</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Wed, 16 Nov 2022 12:08:35 +0000</pubDate>
      <link>https://dev.to/danielstai/how-to-connect-a-react-frontend-to-an-express-server-270h</link>
      <guid>https://dev.to/danielstai/how-to-connect-a-react-frontend-to-an-express-server-270h</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post we will create a skeleton react app and connect it to an express rest API. &lt;/p&gt;

&lt;p&gt;This is intended to be an entry point for beginner frontend or backend devs who want a sneak peek into how they can build fullstack apps. &lt;/p&gt;

&lt;p&gt;Without further ado, lets get right into it&lt;/p&gt;

&lt;h2&gt;
  
  
  Project setup
&lt;/h2&gt;

&lt;p&gt;We will create a folder called &lt;code&gt;fullstack&lt;/code&gt; which will host both our client and server apps. In your terminal or through your OS's GUI, create the following project structure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yO6XoRTo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dmypqwsj8nhummr0xey.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yO6XoRTo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dmypqwsj8nhummr0xey.png" alt="project structure" width="296" height="198"&gt;&lt;/a&gt;&lt;br&gt;
Lets set up the server side code first. &lt;/p&gt;
&lt;h2&gt;
  
  
  Server code
&lt;/h2&gt;

&lt;p&gt;In your code editor of choice, &lt;code&gt;cd&lt;/code&gt; into the server directory and run &lt;code&gt;npm init -y&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This will initialize a node project for us and create a package.json file.&lt;/p&gt;

&lt;p&gt;I have written a previous article on how to set up a basic express server and so we delve into the details of that. You can &lt;a href="https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i"&gt;read that article here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In your server dierctory, create a &lt;code&gt;server.js&lt;/code&gt; file. Make sure you update your &lt;code&gt;package.json&lt;/code&gt; file to reflect that the entry point to our server is &lt;code&gt;server.js&lt;/code&gt; rather than the default &lt;code&gt;index.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is the code we have in our server.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;extended&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;names&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Daniel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Theodore&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jackson&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running on localhost: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As per the above code block, you need express, cors, bodyparser and nodemon installed. All the details you'll need to get your simple express server up and running can be &lt;a href="https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i"&gt;found here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Let's look at one code bock though:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;names&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Daniel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Theodore&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jackson&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet from our &lt;code&gt;server.js&lt;/code&gt; file, we are setting the route &lt;code&gt;/api&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When the client app sends a GET request to that route, the response will be a json object &lt;code&gt;names&lt;/code&gt; which is an array of names as seen above. &lt;/p&gt;

&lt;h2&gt;
  
  
  Browser code
&lt;/h2&gt;

&lt;p&gt;We will now create a react front end that will connect to our express server and display our &lt;code&gt;names&lt;/code&gt; object to the screen.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd&lt;/code&gt; into our &lt;code&gt;client&lt;/code&gt; directory and run the command &lt;code&gt;npx create-react-app .&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This will create a react app in the current directory in this case the &lt;code&gt;client&lt;/code&gt; directory. &lt;/p&gt;

&lt;p&gt;To start the development version of your react app in the browser, run &lt;code&gt;npm start&lt;/code&gt; in the terminal&lt;/p&gt;

&lt;p&gt;Lets go ahead and get rid of the contents of App.css as we wont be needing them. In App.js, lets also just remain with a boilerplate component as seen below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fhfp-gfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz6rut2o4swc7kcbdlzb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fhfp-gfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lz6rut2o4swc7kcbdlzb.png" alt="app.js" width="880" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will start by using the &lt;code&gt;useState&lt;/code&gt; hook to define &lt;code&gt;backendData&lt;/code&gt; and &lt;code&gt;setBackendData&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We will then use the &lt;code&gt;useEffect&lt;/code&gt; hook to pull in the data using Fetch API. &lt;/p&gt;

&lt;p&gt;You can read more about react hooks &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we will define how we want our data rendered our browser. We do this inside the div tag in the return statement in App.js.&lt;/p&gt;

&lt;p&gt;We've done so much but this is the entirety of our App.js file in our &lt;code&gt;client/src&lt;/code&gt; directory so far&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;backendData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setBackendData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([{}])&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:4000/api&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;setBackendData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},[])&lt;/span&gt;


  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;backendData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Loading&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;):(&lt;/span&gt;
        &lt;span class="nx"&gt;backendData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save your App.js file and navigate to &lt;code&gt;http://localhost:3000&lt;/code&gt;if not already open&lt;/p&gt;

&lt;p&gt;Let's check our react app in the browser and see if we have managed to display our names list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yt07TNDM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uxm906u9bnedn9tqkt5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yt07TNDM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uxm906u9bnedn9tqkt5.png" alt="react app" width="880" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! &lt;/p&gt;

&lt;p&gt;We have managed to build a skeleton react app and a simple express server and integrated them. &lt;/p&gt;

&lt;p&gt;To confirm that the fetch request we are making in our &lt;code&gt;useEffect&lt;/code&gt; hook works, lets check out the network tab in our browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AmN7F6gG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3vcp6i9uzavhnpuh3op.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AmN7F6gG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3vcp6i9uzavhnpuh3op.png" alt="network" width="880" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Success! &lt;/p&gt;

&lt;p&gt;The fetch request is successful and returns a json object which is displayed on the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The concepts discussed above are basic react and express concepts that can be built upon to create powerful apps that use Javascript for the whole stack. &lt;/p&gt;

&lt;p&gt;To further build on the knowledge learnt here, we will create a React frontend for the express REST API we created in &lt;a href="https://dev.to/danielstai/node-express-basics-part-ii-building-a-restful-api-3fj9"&gt;this article&lt;/a&gt; in my next article.&lt;/p&gt;

&lt;p&gt;Till then, adios muchachos&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>Node + Express Basics Part III: Testing a RESTful API with Postman</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Tue, 01 Nov 2022 14:21:02 +0000</pubDate>
      <link>https://dev.to/danielstai/node-express-basics-part-iii-testing-a-restful-api-with-postman-4c2b</link>
      <guid>https://dev.to/danielstai/node-express-basics-part-iii-testing-a-restful-api-with-postman-4c2b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Part II of our "REST API with Express" series, we built a simple REST API with Express.js.&lt;/p&gt;

&lt;p&gt;In this article we will finish the API development process by testing if our API works as desired using Postman&lt;/p&gt;

&lt;p&gt;Postman is an API platform for developers to design, build, test and iterate their APIs.&lt;/p&gt;

&lt;p&gt;Go ahead an install Postman as per your operating system requirements. You can get instructions on how to do so &lt;a href="https://www.postman.com/downloads/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For Linux users, we will install through the snappy packaging system by running the following in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo snap install postman
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Postman will be installed as a desktop app built with Electron for all operating systems. &lt;/p&gt;

&lt;p&gt;Finish setting up your account and open the desktop app&lt;/p&gt;

&lt;p&gt;You can check out the code we will be testing here on &lt;a href="https://github.com/DanielStai/basic_express_rest_api"&gt;my github&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;We will test the five endpoints we made in the previous article&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Getting all notes
&lt;/h2&gt;

&lt;p&gt;As you may recall from the previous article, we built an endpoint to fetch all the notes from our array (acting as our data storage at this point). &lt;/p&gt;

&lt;p&gt;Lets see whether it serves up the three notes we saved in our &lt;code&gt;notes&lt;/code&gt; array. &lt;/p&gt;

&lt;p&gt;Get the server up and running by navigating to our project folder that contains &lt;code&gt;note-api.js&lt;/code&gt; and run &lt;code&gt;npm run dev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You should see the following in your terminal: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_qIrHnNb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rotj9z2ggn4vatx02n2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_qIrHnNb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rotj9z2ggn4vatx02n2.png" alt="terminal" width="644" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fire up Postman and open up a new tab. &lt;/p&gt;

&lt;p&gt;Select &lt;code&gt;GET&lt;/code&gt; as the method you want to test. In the entry box alongside, type the URL to your API which in this case is &lt;a href="http://localhost:4000/notes"&gt;http://localhost:4000/notes&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This is what we have so far:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mpCQt3mA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipba3mzc7f3pqoi2gdt3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mpCQt3mA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipba3mzc7f3pqoi2gdt3.png" alt="get all notes" width="880" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click on send, we will get the following response:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fiwOHFun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgbxx2qt48cn5q3l4h38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fiwOHFun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgbxx2qt48cn5q3l4h38.png" alt="get all notes response" width="880" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Which works as expected by returning all notes in our array as a JSON object.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Getting an individual note
&lt;/h2&gt;

&lt;p&gt;The process is similar to the process of retrieving all notes. &lt;/p&gt;

&lt;p&gt;Lets try to get back the note with a &lt;code&gt;noteid&lt;/code&gt; of 1. &lt;/p&gt;

&lt;p&gt;Type in http:localhost:4000/note/1 as the URL we want to send our request to and &lt;code&gt;GET&lt;/code&gt; as the HTTP method. &lt;/p&gt;

&lt;p&gt;Click on send and this is the response we get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LhyvwJ8V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du5vjujnlkftngnajt5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LhyvwJ8V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du5vjujnlkftngnajt5f.png" alt="single note" width="880" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happens if we try to get a note with an id that doesn't exist? &lt;/p&gt;

&lt;p&gt;Go ahead and replace the previous query with http:localhost:4000/note/4. &lt;/p&gt;

&lt;p&gt;Note that in our notes array, no note exists with a &lt;code&gt;noteid&lt;/code&gt; of 4. This is the response we get which is in line with our API logic:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T1dkR7gO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d58ti1l1z9t2u1n12v46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T1dkR7gO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d58ti1l1z9t2u1n12v46.png" alt="note not found" width="880" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fantastic!!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating a note
&lt;/h2&gt;

&lt;p&gt;In our API we defined an endpoint for us to create a new note. Lets see if this &lt;code&gt;POST&lt;/code&gt; method works correctly. &lt;/p&gt;

&lt;p&gt;In the same postman tab, navigate from &lt;code&gt;GET&lt;/code&gt; to &lt;code&gt;POST&lt;/code&gt; using the drop-down menu. On the panel just below, move your selection from 'Params' to 'Body'. Also click on 'raw' and input type as JSON. In the entry box below, lets make a new entry with id = 4.&lt;/p&gt;

&lt;p&gt;That's a lot to take in but this is what we have so far:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dCwmVFl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkuo9d7yo3o3xdm2u945.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dCwmVFl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkuo9d7yo3o3xdm2u945.png" alt="new note" width="880" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click on 'Send', we have succesfully created a new note. To confirm this, lets &lt;code&gt;GET&lt;/code&gt; all notes and see if our note has been created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TtSl8ird--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7utfapkkvfx1gw3gwzr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TtSl8ird--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7utfapkkvfx1gw3gwzr.png" alt="confirm new note" width="880" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! We now have 4 notes from our previous 3 notes. &lt;/p&gt;

&lt;p&gt;You can also confirm by invoking the &lt;code&gt;GET&lt;/code&gt; method on our endpoint and see whether you get a response back with our newly created note&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Editing a note
&lt;/h2&gt;

&lt;p&gt;Editing a note is almost the same as posting a new note. We will simply use the POST method and specify the current &lt;code&gt;noteid&lt;/code&gt; of our note as the same as edited noteid. &lt;/p&gt;

&lt;p&gt;Not making much sense? &lt;/p&gt;

&lt;p&gt;Lets see what all the above looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NOmYVkbm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylkvtwg9m7gozi6mockz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NOmYVkbm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylkvtwg9m7gozi6mockz.png" alt="editing note" width="880" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are editing the note with id = 1.&lt;/p&gt;

&lt;p&gt;To confirm the edits have been done after clicking send, lets &lt;code&gt;GET&lt;/code&gt; the note with &lt;code&gt;noteid&lt;/code&gt; = 1:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HAU5Xr3s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flj1sy8r1qvbz8nidfrr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HAU5Xr3s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flj1sy8r1qvbz8nidfrr.png" alt="confirm edit" width="880" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Edit done!&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Deleting a note
&lt;/h2&gt;

&lt;p&gt;So far we have created a note and saved it in our notes array, retrieved all notes, retrieved an individual note and edited a specific note.&lt;/p&gt;

&lt;p&gt;We will finish our tests by testing our deletion endpoint that we created in &lt;code&gt;note-api.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As per our API, we simply need to specify the HTTP method as &lt;code&gt;DELETE&lt;/code&gt; and then specify the &lt;code&gt;noteid&lt;/code&gt; of the note we want to delete.&lt;/p&gt;

&lt;p&gt;Lets see all that in action below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NX0EItsd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94cjppppax1p2prstw6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NX0EItsd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94cjppppax1p2prstw6h.png" alt="delete note" width="880" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click 'send', our note is deleted and we get a response back from the server that 'note has deleted'.&lt;/p&gt;

&lt;p&gt;To confirm our data has been deleted, lets GET the note with id = 1 and see if it has been deleted. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mGcrSILa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0r7txvry833kuy8skqd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mGcrSILa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0r7txvry833kuy8skqd.png" alt="confirm delete" width="880" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The note has been successfully deleted as expected&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By the end of Part III of our three part series of articles, we have created a basic HTTP server with express, extended it to a REST API and finally tested it with Postman.&lt;/p&gt;

&lt;p&gt;Congratulations to us! In our next series (Node + Express Intermediate), we will dive deeper into middleware and take advantage of middleware to build better REST and GraphQL APIs.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Node + Express Basics Part II: Building a RESTful API</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Tue, 01 Nov 2022 14:20:43 +0000</pubDate>
      <link>https://dev.to/danielstai/node-express-basics-part-ii-building-a-restful-api-3fj9</link>
      <guid>https://dev.to/danielstai/node-express-basics-part-ii-building-a-restful-api-3fj9</guid>
      <description>&lt;p&gt;This article is Part II in our ongoing three Part series: Node + Express Basics. &lt;/p&gt;

&lt;p&gt;In Part I, we built a basic HTTP server and defined what middleware is, which is how express manages to abstract out all the details of dealing with requests and responses.&lt;/p&gt;

&lt;p&gt;In this article we will define what REST APIs are and what they do. &lt;/p&gt;

&lt;p&gt;We will then go ahead to extend our basic server from &lt;a href="https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i"&gt;Part I&lt;/a&gt; into a fully fledged REST API. &lt;/p&gt;

&lt;p&gt;What is a REST API? &lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://aws.amazon.com/what-is/api/"&gt;this article by Amazon&lt;/a&gt;, REST stands for Representational State Transfer and defines a set of functions like GET, PUT, DELETE, etc. that clients can use to access server data. Clients and servers exchange data using HTTP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The main feature of REST API is statelessness&lt;/strong&gt;. Statelessness means that servers do not save client data between requests. &lt;/p&gt;

&lt;p&gt;Client requests to the server are similar to URLs you type in your browser to visit a website. &lt;/p&gt;

&lt;p&gt;The response from the server is plain data, without the typical graphical rendering of a web page.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Project set up
&lt;/h2&gt;

&lt;p&gt;What are we building? &lt;/p&gt;

&lt;p&gt;We will build a REST API that'll serve a front end notebook application. We will build our REST API with end points that correspond to the HTTP methods of GET, POST, PUT/PATCH and DELETE. &lt;/p&gt;

&lt;p&gt;We will do that by extending the basic express HTTP server we made in the &lt;a href="https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i"&gt;previous example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will then test our API using Postman in Part III.&lt;/p&gt;

&lt;p&gt;This is the code we have so far from the previous article. For this tutorial, create a new file and lets name it &lt;code&gt;note-api.js&lt;/code&gt; and paste the following starter code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;bodyParser&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body-parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bodyParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;extended&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In setting up our project in Part I, we installed &lt;code&gt;express&lt;/code&gt; and &lt;code&gt;cors&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We will now install some more middleware and in this case we will install the middleware &lt;code&gt;bodyParser&lt;/code&gt;. As the name suggests, we will be using this middleware to get information from the request body of our API calls. &lt;/p&gt;

&lt;p&gt;To install &lt;code&gt;bodyparser&lt;/code&gt;, go ahead and run the following in our project folder in the terminal&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 bodyparser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last three lines of the example code above illustrate how to load our middleware. &lt;/p&gt;

&lt;p&gt;In general, we use &lt;code&gt;app.use(&amp;lt;middleware function&amp;gt;)&lt;/code&gt; when mounting middleware at root level i.e. to be used on all requests. &lt;/p&gt;

&lt;p&gt;If we wanted to say for example mount middleware for post requests only, we would use &lt;code&gt;app.post(&amp;lt;middleware function&amp;gt;)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now that our app is correctly set up, lets go ahead an start building our API.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Defining data storage
&lt;/h2&gt;

&lt;p&gt;For our API, we will make use of temporary data stored in an array. Take note of the temporary nature of the data. This means that any changes or modification done to our array will cease to exist once we restart our server. &lt;/p&gt;

&lt;p&gt;Here's a sample of one entry in our array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;noteid:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;title:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'First&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Note'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;author:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'Daniel&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Stai'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;content:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'This&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;my&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;very&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;first&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;note&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;ever'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;date_created:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;2022&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see the various fields one note entry has. Lets go ahead and create an empty array called &lt;code&gt;notes&lt;/code&gt; and add 3 sample notes to work with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
    &lt;span class="na"&gt;noteid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;First Note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Daniel Stai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is my very first note ever&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;date_created&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2022/10/28&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;noteid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Second Note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jack Sparrow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is my second note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;date_created&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2022/10/28&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;noteid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Third Note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Aegon Targaryen&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is my second note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;date_created&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2022/10/28&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In subsequent articles, we will make our data persist by using a database such as MongoDB and also generate unique ids to our entries for ease of access. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Getting all the notes
&lt;/h2&gt;

&lt;p&gt;Now that we have some sample data to work with, lets go ahead and add a few endpoints to our API.&lt;/p&gt;

&lt;p&gt;As we saw in our REST API definition, the requests made by the client to the server (the endpoints) are similar to the URLs we normally type in a browser.&lt;/p&gt;

&lt;p&gt;Our server is currently running on port 4000 so what happens when one sends a &lt;code&gt;GET&lt;/code&gt; request to &lt;code&gt;http:localhost:4000/notes&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;Ideally we want them to get back a list of all the notes currently in our array. Lets go ahead and write that route. &lt;/p&gt;

&lt;p&gt;Express routes are of the form &lt;code&gt;app.METHOD(PATH, HANDLER)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;METHOD&lt;/code&gt; is the corresponding HTTP method in lowercase, &lt;code&gt;PATH&lt;/code&gt; is the relative path in our server and &lt;code&gt;HANDLER&lt;/code&gt; is the function that Express will call once our route is matched. &lt;/p&gt;

&lt;p&gt;In our case to retrieve all notes we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/notes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to retrieve a single note, we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/note/:noteid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;noteid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;noteid&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;note&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;noteid&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;noteid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Note not found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code block above, when for example we get a get request from &lt;code&gt;say http://localhost:4000/note/1&lt;/code&gt;, we extract the note id using&lt;code&gt;req.params.id&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This enables us to use this id value in the request body to check whether there is a corresponding match in our array &lt;code&gt;notes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If such a match exists, we return that particular note. If it doesn't exist, we return a &lt;code&gt;404 error&lt;/code&gt; with the custom message that 'Note not found'.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating a new note
&lt;/h2&gt;

&lt;p&gt;To create a new note, lets create a skeleton html file that we will use to add data to our &lt;code&gt;notes&lt;/code&gt; array. Create a new file called &lt;code&gt;new-note.html&lt;/code&gt; and paste the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;hr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Create New Note&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;hr&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"http://localhost:4000/note"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-group"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"noteid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Note ID&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-control"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"noteid"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-group"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Title&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-control"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-group"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Author"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Author&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-control"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"author"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-group"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Content&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-control"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn btn-primary"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wont go to the details of the html code but if you run the live preview on vscode or go to &lt;code&gt;http://localhost:3000/new-note.html&lt;/code&gt; on your browser, you'll see our form that we'll use to post a new note entry. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uU97YUQS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1fap9mzpkbc8zmxfh41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uU97YUQS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1fap9mzpkbc8zmxfh41.png" alt="new note html" width="651" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Back in our &lt;code&gt;note-api.js&lt;/code&gt; file, lets deal with the logic of making this happen. Following the same structure for our get requests, lets write our post request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;note&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Note added to notebook&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will assign the request body i.e. &lt;code&gt;req.body&lt;/code&gt; to a variable &lt;code&gt;note&lt;/code&gt; and then push this new note to our already existing notes array using &lt;code&gt;notes.push(note)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We also log the new note that has been created to the console. This might help when debugging potential issues.&lt;/p&gt;

&lt;p&gt;When that is successful, we will send back a response i.e. 'Note added to notebook'. &lt;/p&gt;

&lt;p&gt;With your server running, go ahead and fill our form. We will get the following if successful:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ef9n5Fw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhtgm5jyg1m90u5ghp4r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ef9n5Fw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhtgm5jyg1m90u5ghp4r.png" alt="new note confirmation" width="718" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Editing a note
&lt;/h2&gt;

&lt;p&gt;Once you have created a note, you might need to edit the contents of the note. Let's add that logic to our API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/note/:noteid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;noteid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;noteid&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;note&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isbn&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;isbn&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNote&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Note has been edited&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will assign the variables &lt;code&gt;noteid&lt;/code&gt; and &lt;code&gt;newNote&lt;/code&gt; to the note id and request body respectively. &lt;/p&gt;

&lt;p&gt;We will then loop through our notes and check if there exists a note with a matching id to the note we want to edit. If it exists, we replace its contents with &lt;code&gt;newNote&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We also send back a response of 'Note has been edited'.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Deleting a note
&lt;/h2&gt;

&lt;p&gt;So far we've dealt with creating a new note, retrieving an individual note, retrieving all notes and editing an existing note. &lt;/p&gt;

&lt;p&gt;The last part of the logic of our API will deal with deleting a note from our data storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/note/:noteid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;noteid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;noteid&lt;/span&gt;

    &lt;span class="nx"&gt;notes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;noteid&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;noteid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Note has been deleted&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With deletion, we need to be careful so as not to inadvertently delete all records in our data store. &lt;/p&gt;

&lt;p&gt;To do so, we will filter our notes array looking for a record with a note id equivalent to the note id in the request.&lt;/p&gt;

&lt;p&gt;Once found, that record is filtered out, returning the filtered array without the desired record.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;And with that we have a fully functional RESTful API. How do we know it does what it says it does? In Part III, we will test our API using Postman. You can go ahead and &lt;a href="https://dev.to/danielstai/node-express-basics-part-iii-testing-a-restful-api-with-postman-4c2b"&gt;read the article here&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Node + Express Basics Part I: Building a basic HTTP server</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Tue, 25 Oct 2022 03:42:22 +0000</pubDate>
      <link>https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i</link>
      <guid>https://dev.to/danielstai/how-to-set-up-a-basic-http-server-in-node-express-10i</guid>
      <description>&lt;p&gt;This article is Part I of a three part series in which we will use node + express to learn how to build a RESTful API and test it with Postman.&lt;/p&gt;

&lt;p&gt;In Part I we will have a look at how to set up a basic HTTP server with Node and Express&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We can define a &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_web_server"&gt;HTTP server&lt;/a&gt; as software that understands URLs (web addresses) and HTTP (the protocol your browser uses to view web pages). &lt;/p&gt;

&lt;p&gt;A HTTP server can be accessed through the domain names of the websites it stores, and it delivers the content of these hosted websites to the end user's device.&lt;/p&gt;

&lt;p&gt;Node is a JavaScript runtime environment that helps us run JavaScript outside the browser. Express is a Node.js framework that makes it easier for us to develop APIs with node, which by itself isn't as friendly for development&lt;/p&gt;

&lt;p&gt;You can find the code for this on &lt;a href="https://github.com/DanielStai/http_server"&gt;my Github&lt;/a&gt;. Make sure you follow me there too :)&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Setting up the project
&lt;/h2&gt;

&lt;p&gt;Before we begin, you will need to install Node.js on your system and set up a local development environment. &lt;/p&gt;

&lt;p&gt;To do this, you can follow along &lt;a href="https://www.digitalocean.com/community/tutorial_series/how-to-install-node-js-and-create-a-local-development-environment"&gt;this tutorial&lt;/a&gt; which describes how to install Node.js for different operating systems.&lt;/p&gt;

&lt;p&gt;Once you've installed node (and &lt;code&gt;npm&lt;/code&gt; by extension), create a directory for the project and &lt;code&gt;cd&lt;/code&gt; into it. I'll call my directory &lt;code&gt;node_example&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir node_example &amp;amp;&amp;amp; cd node_example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once in our projects directory, we will first run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to create a package.json file. &lt;/p&gt;

&lt;p&gt;We can now go ahead to install the required dependencies. In the terminal, go ahead and run&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 cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install Express for us and also install the middleware CORS. &lt;/p&gt;

&lt;p&gt;CORS stands for cross origin resource sharing. The CORS package we are installing provides an Express middleware that enables CORS with different options so we can make the right connections on the network.&lt;/p&gt;

&lt;p&gt;While we are at it, lets also install nodemon as a dev dependency. &lt;/p&gt;

&lt;p&gt;Why nodemon?&lt;/p&gt;

&lt;p&gt;Every time you make changes to your files, we will need to restart the server for those changes to take effect. &lt;/p&gt;

&lt;p&gt;Nodemon takes care of that for us, saving us from the cumbersome task of always starting and restarting the server. Go ahead and run the following in the terminal&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 -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install nodemon as a dev dependency. &lt;/p&gt;

&lt;p&gt;Dev dependencies are packages that are installed to aid in the development process hence not needed in the production environment&lt;/p&gt;

&lt;p&gt;Now we need to edit the package.json file so as to ensure &lt;code&gt;npm start&lt;/code&gt; starts the server. We will do this by adding &lt;code&gt;"start":"node index.js"&lt;/code&gt; to the &lt;code&gt;scripts&lt;/code&gt; as shown below. Note that we will be creating our server in &lt;code&gt;index.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodemon index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"node index.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have also created a script named &lt;code&gt;dev&lt;/code&gt; that runs &lt;code&gt;nodemon index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To use &lt;code&gt;import&lt;/code&gt; statements in &lt;code&gt;index.js&lt;/code&gt; instead of the usual 'require', we add a &lt;code&gt;type&lt;/code&gt; of &lt;code&gt;module&lt;/code&gt; to our package.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Writing our server code
&lt;/h2&gt;

&lt;p&gt;We are now all set to start writing code to create our server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt;  &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code block above, we will import express and cors using the import statement. &lt;/p&gt;

&lt;p&gt;We can import like this since we added &lt;code&gt;"type":"module"&lt;/code&gt; to our package.json file.&lt;/p&gt;

&lt;p&gt;We then create an express application by invoking &lt;code&gt;express()&lt;/code&gt; and designate a port number in our &lt;code&gt;port&lt;/code&gt; variable. &lt;/p&gt;

&lt;p&gt;We will hard code our port number now but in subsequent examples we will point the port variable to an environment variable in the &lt;code&gt;.env&lt;/code&gt; file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const port = process.env.VARIABLE_NAME || 4000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will tell our application to assign the variable name &lt;code&gt;port&lt;/code&gt; to an environment variable &lt;code&gt;VARIABLE_NAME&lt;/code&gt; stored in our &lt;code&gt;.env&lt;/code&gt; file and default to 4000 if no such environment variable exists in the &lt;code&gt;.env&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Configuring middleware
&lt;/h2&gt;

&lt;p&gt;In the next code block, we will configure some middleware.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;extended&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will not take a deep dive into middleware for now but we will define middleware as functions that execute some code that can have side effects on the app, and usually add information to the request or response objects.&lt;/p&gt;

&lt;p&gt;The syntax used to load middleware is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(&amp;lt;middleware_function&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This mounts the middleware at root level. &lt;/p&gt;

&lt;p&gt;There are other ways to mount the middleware to specific routes or to a specific type of request but those are beyond the scope of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Defining app routes
&lt;/h2&gt;

&lt;p&gt;We will now define our app routes and specifically what gets sent with a get request to the root of our app ('/').&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Express routes can be expressed (pun intended) as &lt;code&gt;app.METHOD(PATH, HANDLER)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;METHOD is our http method in lowercase, PATH is our relative path on the server and HANDLER is the function that express calls when the route is matched.&lt;/p&gt;

&lt;p&gt;We then use &lt;code&gt;app.listen()&lt;/code&gt; to define the port that our API server should be listening on&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Running the server
&lt;/h2&gt;

&lt;p&gt;And that's it. We have written all the code we need to create a basic http server. &lt;/p&gt;

&lt;p&gt;To run our server, type &lt;code&gt;npm run dev&lt;/code&gt; in your terminal. If you followed all steps correctly, you should see &lt;code&gt;Server is running on port 4000&lt;/code&gt; in your terminal.&lt;/p&gt;

&lt;p&gt;On your browser, visit &lt;code&gt;http://localhost:4000&lt;/code&gt; and you'll see 'Hello World' logged on the screen&lt;/p&gt;

&lt;p&gt;In the next article we will define REST APIs and extend this example to a fully fledged REST API. You can &lt;a href="https://dev.to/danielstai/node-express-basics-part-ii-building-a-restful-api-3fj9"&gt;read the article here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>express</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Getting Started with the Command Line Part 2: Pipes and Redirection</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Thu, 13 Oct 2022 15:30:24 +0000</pubDate>
      <link>https://dev.to/danielstai/getting-started-with-the-command-line-part-2-pipes-and-redirection-3kb9</link>
      <guid>https://dev.to/danielstai/getting-started-with-the-command-line-part-2-pipes-and-redirection-3kb9</guid>
      <description>&lt;p&gt;This article is part of a series of articles aimed at getting you, the complete beginner at using the command line, from zero to hero. &lt;/p&gt;

&lt;p&gt;This article is a follow up to Command Line basics Part 1: Intro to the Command Line which you can read &lt;a href="https://dev.to/danielstai/getting-started-with-the-command-line-part-1-22bi"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The whole series is based on Meta's Backend developer course (Version Control) on Coursera. You can have a look at the course &lt;a href="https://www.coursera.org/learn/introduction-to-version-control" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the end of this article you'll be able to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define what a pipe is and how to use it to be more effective on the terminal&lt;/li&gt;
&lt;li&gt;Define what redirection is and a use redirection on standard input, standard output and standard error&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pipes
&lt;/h2&gt;

&lt;p&gt;In the previous article, we went through a few basics commands such as &lt;code&gt;cd&lt;/code&gt;, &lt;code&gt;ls&lt;/code&gt;, &lt;code&gt;pwd&lt;/code&gt; and &lt;code&gt;mkdir&lt;/code&gt; among others.&lt;/p&gt;

&lt;p&gt;We also saw a very simple example of a typical workflow using the command line.&lt;/p&gt;

&lt;p&gt;In this article we will take it a little further by discussing pipes. &lt;/p&gt;

&lt;p&gt;We use pipes to take the output from one command as the input to another. &lt;/p&gt;

&lt;p&gt;The pipe symbol is &lt;code&gt;|&lt;/code&gt;. We will use the steps below to illustrate its use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1. Create sample files to use&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will first create the files we will work on and add some dummy 'lorem ipsum' text to help illustrate our point. This work flow can be seen in the screen grab below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvi3mt137ywe7lpbmj6eu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvi3mt137ywe7lpbmj6eu.png" alt="CLI workflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have created two files i.e. example1.txt and example2.txt. The command cat &lt;code&gt;example1.txt&lt;/code&gt; and &lt;code&gt;cat exampe2.txt&lt;/code&gt; simple shows us the content of the text file&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2. Using a pipe to open the text file and count the words in the file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will now use a pipe (&lt;code&gt;|&lt;/code&gt;) to open the text files and count the words in the file. &lt;/p&gt;

&lt;p&gt;We can simply run the command &lt;code&gt;wc cat example1.txt -w&lt;/code&gt; and similarly for example2.txt. The &lt;code&gt;-w&lt;/code&gt; flag tells the command to return the word count for the example1.txt file. &lt;/p&gt;

&lt;p&gt;Say we want the combined word count of example1.txt and example2.txt? &lt;/p&gt;

&lt;p&gt;This is where pipes come in. We will run the command &lt;code&gt;cat example1.txt example2.txt | wc -w&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;All the above steps can be seen in the screen grab below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1qrzwsd8z5xmwhyhf3ir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1qrzwsd8z5xmwhyhf3ir.png" alt="word count"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully used a pipe to take the output of one command (&lt;code&gt;cat example1.txt example2.txt&lt;/code&gt;) as the input of the next command (&lt;code&gt;wc -w&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Lets take a look at redirection now&lt;/p&gt;

&lt;h2&gt;
  
  
  Redirection
&lt;/h2&gt;

&lt;p&gt;As you may have already noticed, the standard workflow of any linux command is that it takes an input and gives some sort of output or error message. &lt;/p&gt;

&lt;p&gt;In most cases, the standard input device is the keyboard while the standard output device is the screen/monitor. &lt;/p&gt;

&lt;p&gt;Using redirection, we can change where the standard input comes from or where output and errors go.&lt;/p&gt;

&lt;p&gt;We will take a look at the three io (input/output) redirections available i.e. standard input redirection, standard output redirection and error redirection&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Standard input redirection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The standard input redirection gives you the option to record your input and save it to a file either by overwriting or appending the file. &lt;/p&gt;

&lt;p&gt;Lets see this using the example in the image below. We will continue working in the 'tutorial' folder we created in the previous section when discussing pipes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhx8gs8jv7cwy8h4nwlwp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhx8gs8jv7cwy8h4nwlwp.png" alt="stdin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First we check the existing contents of the 'tutorial' folder by running the already familiar &lt;code&gt;ls&lt;/code&gt; tag. We can see our two files from earlier&lt;/p&gt;

&lt;p&gt;For user input, the symbol &lt;code&gt;&amp;lt;&lt;/code&gt; is used. But first, type the command &lt;code&gt;cat &amp;gt; input.txt&lt;/code&gt;. This will create the file 'input.txt' and assign whatever text you type in the console to it. Press CTRL + D to exit the input prompt&lt;/p&gt;

&lt;p&gt;Now lets check the contents of the 'tutorial' folder. You can see a new file 'input.txt' has been created. To see the content of the file, we can type in the command &lt;code&gt;cat &amp;lt; input.txt&lt;/code&gt;. This will display the text in 'input.txt' that we had typed in earlier.&lt;/p&gt;

&lt;p&gt;The above workflow can be seen below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1u17bmmm3j90t0zkpwn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1u17bmmm3j90t0zkpwn.png" alt="input.txt exists"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Standard Output redirection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The redirection of standard output allows you to control where output goes. &lt;/p&gt;

&lt;p&gt;When you run a simple command like &lt;code&gt;ls -l&lt;/code&gt;, the output is normally some text that we can see on the terminal. &lt;/p&gt;

&lt;p&gt;We could choose to store this information in a text file for whatever reason. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7wyj4gi3y7adq2zlcq5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7wyj4gi3y7adq2zlcq5f.png" alt="output.txt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will simply run the command &lt;code&gt;ls -l &amp;gt; output.txt&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This will take the output from the &lt;code&gt;ls -l&lt;/code&gt; command, create a file called 'output.txt' and write the output there. &lt;/p&gt;

&lt;p&gt;Running the &lt;code&gt;ls&lt;/code&gt; command we can confirm that indeed the file 'output.txt' has been created. To view the contents of the file, we run the command &lt;code&gt;less output.txt&lt;/code&gt; in the terminal. &lt;/p&gt;

&lt;p&gt;This is what we get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsebduexy7peg5uclgp86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsebduexy7peg5uclgp86.png" alt="output.txt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, our 'output.txt' file contains the same message we saw on our terminal when we ran &lt;code&gt;ls -l&lt;/code&gt;. Redirection successful!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Standard error redirection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The standard error redirect allows you to specify that the error should be written to a file.&lt;/p&gt;

&lt;p&gt;As in previous example, lets do it using a real example&lt;/p&gt;

&lt;p&gt;Still in the 'tutorial' directory, lets try to open a file/directory that we know obviously doesn't exist there. We are trying to simulate an error message. &lt;/p&gt;

&lt;p&gt;We run the command &lt;code&gt;ls -l /bin/usr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will get the error message that the file/directory doesn't exist. &lt;/p&gt;

&lt;p&gt;Our goal is to redirect and write this error message to a text file thus we run the command &lt;code&gt;ls -l /bin/usr 2&amp;gt; error.txt&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;2&amp;gt;&lt;/code&gt; is the symbol used to redirect error output. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Femkjfe52rrde06rmak9h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Femkjfe52rrde06rmak9h.png" alt="error.txt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check the contents of the folder to confirm that indeed the file 'error.txt' has been created. To see its contents, run &lt;code&gt;less error.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5t02sutju3ffa4bverl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5t02sutju3ffa4bverl.png" alt="less error.txt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are instances where you are not sure whether you will get actual output from the command or an error. &lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;2&amp;gt;&amp;amp;1&lt;/code&gt; comes in. Run the command &lt;code&gt;ls -l /bin/usr &amp;gt; error_output.txt 2&amp;gt;&amp;amp;1&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This will create and write the output or error to the file 'error_output.txt'.&lt;/p&gt;

&lt;p&gt;This brings us to the end of this article on using pipes and redirection with Unix commands. &lt;/p&gt;

&lt;p&gt;I hope you've learnt something new or brushed up on some knowledge. &lt;/p&gt;

&lt;p&gt;In part 3 of our Command Line Basics series we will take a look at Grep (Global regular expression print). &lt;/p&gt;

&lt;p&gt;The standard input redirection gives you the option to record your input and save it to a file either by overwriting or appending the file&lt;/p&gt;

&lt;p&gt;X&lt;/p&gt;

&lt;p&gt;The standard error redirect allows you to specify that the error should be written to a file.     &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>linux</category>
    </item>
    <item>
      <title>How I Contributed to my first Open Source project</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Sat, 01 Oct 2022 23:21:31 +0000</pubDate>
      <link>https://dev.to/danielstai/how-i-contributed-to-my-first-open-source-project-457p</link>
      <guid>https://dev.to/danielstai/how-i-contributed-to-my-first-open-source-project-457p</guid>
      <description>&lt;p&gt;Many aspiring developers wish to start contributing to open source but they lack a starting point. &lt;/p&gt;

&lt;p&gt;Why is contributing to open source important? &lt;/p&gt;

&lt;p&gt;Contributing to open source is a way to practice your coding skills as well as a chance to work on real life problems and colalborate with others&lt;/p&gt;

&lt;p&gt;In this article, we will go through the steps one can take to contribute to a simple first project. &lt;/p&gt;

&lt;p&gt;Some knowledge of Git and GitHub is required. &lt;/p&gt;

&lt;p&gt;You can get a refresher by reading my article on Git basics &lt;a href="https://dev.to/danielstai/getting-started-with-git-and-github-part-1-intro-to-git-and-github-k7a"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Look for a good first issue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For our first issue, we will use the &lt;a href="https://github.com/firstcontributions/first-contributions"&gt;first contributions&lt;/a&gt; GitHub repository that is specifically built for beginners to practice contributing to open source. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Fork the repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Click on the fork repository button in the screen below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QJzBBbUH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95gvzfppji8zg7nfq36v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QJzBBbUH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95gvzfppji8zg7nfq36v.png" alt="forking repository" width="880" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create a copy of the repository in your GitHub account that you can then work with&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Clone the repository to your local computer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To  have a local copy of the first-contributions repo, run the command &lt;code&gt;git clone https://github.com/firstcontributions/first-contributions.git&lt;/code&gt; in the appropriate local folder. &lt;/p&gt;

&lt;p&gt;You can copy the link to the repository when when you click on 'code' then 'Copy' on the screen below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VHqG6ica--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phj7q8hd5txd2h3esz4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VHqG6ica--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phj7q8hd5txd2h3esz4n.png" alt="code snippet" width="442" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Create a branch&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We now have a copy of the code in our local computer. &lt;/p&gt;

&lt;p&gt;Navigate to that directory so that we can make the necessary modifications.&lt;/p&gt;

&lt;p&gt;We will create a branch from our master branch so that we can work on changes to our code. It is this branch that will push to GitHub, create a pull request and hope its merged to the main branch.&lt;/p&gt;

&lt;p&gt;Let's call our branch 'add_my_name' &lt;/p&gt;

&lt;p&gt;To create the branch and check it out at the same time, we run the command &lt;code&gt;git checkout -b add_my_name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will create the branch and set it as our current working branch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Make the required changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For this repository, the extent of changes we can make is editing the 'Contributors.md' file  to insert our name. &lt;/p&gt;

&lt;p&gt;Open the Contributors.md file and add your name to the file and save it.&lt;/p&gt;

&lt;p&gt;Remember we are working on the branch &lt;code&gt;add_my_name&lt;/code&gt;. Time to submit our changes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Push local changes to GitHub&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the command &lt;code&gt;git push -u add_my_name&lt;/code&gt;. This will sync our local and remote repositories&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Create pull request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to your GitHub page and if you've done everything right, you should see a page with a 'Compare Pull request' button like below. Click it &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PxLafNMc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5o30tlqkebm3o53evzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PxLafNMc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5o30tlqkebm3o53evzw.png" alt="compare PR snippet" width="357" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the 'Create pull request' button as below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wgjduJCv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g6f2atqa4hrr3go4cr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wgjduJCv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g6f2atqa4hrr3go4cr7.png" alt="Pull request creation" width="361" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A pull request is a notification to the owners of the code that you've made some changes to their code and you'd like them to merge them into their projects&lt;/p&gt;

&lt;p&gt;At this point you'll see the following page&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BDCMEvdQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iua8gfu1wlhgazn2t04y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BDCMEvdQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iua8gfu1wlhgazn2t04y.png" alt="contribution merged" width="880" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And its at this stage where you'll celebrate having done your first contribution to an open source project. Congratulations!!&lt;/p&gt;

&lt;p&gt;The purpose of this simple project is to expose you to the tools and the typical workflow of a simple open source contribution.&lt;/p&gt;

&lt;p&gt;In subsequent articles, we will get more familiar with the fork-clone-Edit-PR cycle that is typical in any open source contribution workflow.&lt;/p&gt;

&lt;p&gt;Adios.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>github</category>
      <category>git</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Getting Started with Git and GitHub Part 1: Intro to Git and GitHub</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Fri, 30 Sep 2022 09:14:26 +0000</pubDate>
      <link>https://dev.to/danielstai/getting-started-with-git-and-github-part-1-intro-to-git-and-github-k7a</link>
      <guid>https://dev.to/danielstai/getting-started-with-git-and-github-part-1-intro-to-git-and-github-k7a</guid>
      <description>&lt;p&gt;This article is part of articles targeted at getting you, the complete beginner at using the command line and git for version control, from zero to hero. &lt;/p&gt;

&lt;p&gt;At the end of this article you'll be able to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;outline the git principles &lt;/li&gt;
&lt;li&gt;use a GitHub repository &lt;/li&gt;
&lt;li&gt;describe the steps in a standard GitHub workflow &lt;/li&gt;
&lt;li&gt;create branches and merge different branches&lt;/li&gt;
&lt;li&gt;describe how code goes from local development to version control and then to live production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article is based on Meta's Back-end Developer Certificate Course 3: Version Control. You can find a link to the course &lt;a href="https://www.coursera.org/learn/introduction-to-version-control" rel="noopener noreferrer"&gt;here&lt;/a&gt; and a link to my article on Meta Back-end developer Course 1: Introduction to Back-end Development &lt;a href="https://dev.to/danielstai/introduction-to-backend-development-by-meta-a-review-59fm"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Often many beginners get stuck when starting using git and github due to a failure to properly understand git and failing to appreciate the immense power of git and its usefulness to the software collaboration and development cycle.&lt;/p&gt;

&lt;p&gt;This article will give a brief but concise introduction to git for beginners to enable them to jump right away and start collaborating and building repositories for their portfolios.&lt;/p&gt;

&lt;p&gt;These will the main objectives of this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are Version Control systems and how are they implemented?&lt;/li&gt;
&lt;li&gt;Learn how to navigate and configure version control systems using the command line.&lt;/li&gt;
&lt;li&gt;Learn how to manage code revisions.&lt;/li&gt;
&lt;li&gt;Create and use a GitHub repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Git Principles
&lt;/h2&gt;

&lt;p&gt;In their most basic form, version control systems are borne out of a need to have teams collaborate and work on the same codebase. &lt;/p&gt;

&lt;p&gt;We can define version control more formally as the practice of tracking and managing changes to software. &lt;/p&gt;

&lt;p&gt;Version control systems enable developers to backtrack to previous versions of code if need be. &lt;/p&gt;

&lt;p&gt;This is important given that multiple team members might be working on the same source code and without version control, you cannot tell who made what changes and when. &lt;/p&gt;

&lt;p&gt;A more catastrophic result is having no way to backtrack to a previous version of software when a change to the existing code introduces bugs or crashes the code altogether.&lt;/p&gt;

&lt;p&gt;Benefits of version control:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Long term history of changes&lt;/li&gt;
&lt;li&gt;Branching and merging&lt;/li&gt;
&lt;li&gt;Traceability&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll now go through a basic git workflow that will illustrate most of the basics that will get you started with git and GitHub&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;0.Install git and create a free GitHub account&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install git according to your operating system &lt;a href="https://git-scm.com/downloads" rel="noopener noreferrer"&gt;here&lt;/a&gt; and create your free GitHub account &lt;a href="https://github.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Git is a version control system while GitHub is provides an internet hosting service and a user interface to manage and store our git repositories&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Create a local git repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the folder you want to initialize your git repository. Run the command &lt;code&gt;git init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Creating files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the same folder where you've initialized git, let's create a file and see how git tracks files. Run the command &lt;code&gt;touch example.txt&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;You can review how to create files among other simple command line commands from my earlier article &lt;a href="https://dev.to/danielstai/getting-started-with-the-command-line-part-1-22bi"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;3.Adding files to the staging environment&lt;/p&gt;

&lt;p&gt;You've created a new file but you need to 'add' it to the staging area for git to start tracking changes made to the file. Run the command &lt;code&gt;git add example.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.Create a commit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With a new file already added, we'll now need to commit it thereby saving any changes we'd made on the file. &lt;/p&gt;

&lt;p&gt;Commits are accompanied by a commit message to show what changes have been made and to make it easier to review changes made to a file or project.&lt;/p&gt;

&lt;p&gt;Run the command &lt;code&gt;git commit -m 'Initial commit'&lt;/code&gt;. Here, "initial commit" is the commit message&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.Create a new branch&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you create a repository on git, the branch you work on by default is the main branch, aptly named 'main' or 'master' (to be deprecated soon). &lt;/p&gt;

&lt;p&gt;To work on different features and bugs etc, you create new branches on which you can make changes that can be merged back to the main branch after a pull request. More on pull requests later&lt;/p&gt;

&lt;p&gt;First, you can check which branch you are currently on by typing the command &lt;code&gt;git status&lt;/code&gt;. Then you can use the command &lt;code&gt;git checkout -b feature&lt;/code&gt;. This creates a branch named 'feature' and sets it as the current branch to be worked on&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.Create a new repository on Github&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We already covered how to create a Github account earlier. Creating a Github repository is quite easy as seen below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9com6e6zvvt19szg28e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9com6e6zvvt19szg28e.png" alt="create new repo"&gt;&lt;/a&gt;&lt;br&gt;
GitHub will ask if you want to create a new repo from scratch or push an existing repo. Since we've already created our repo, we'll choose the latter by typing &lt;code&gt;git remote add origin 'link to repo'&lt;/code&gt; Replace 'link to repo' with a link to your repository&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7.Push your branch to GitHub&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Earlier, we created a local git repository, added and committed a file, created a new branch to work on, created a remote GitHub repository. &lt;/p&gt;

&lt;p&gt;Now we would like to push our local branch to GitHub by running &lt;code&gt;git push -u origin feature&lt;/code&gt;. Go to GitHub and you'll see the new branch appear as shown below&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.Create a pull request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Navigating to the page shown below, click on the create pull request button. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6jed1o9mgsctnrsqtf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6jed1o9mgsctnrsqtf0.png" alt="create pull request"&gt;&lt;/a&gt;&lt;br&gt;
A pull request is a way to notify the code owner (in this case you are the code owner) that you want to make changes in the code. Here, the owner reviews the code and if satisfied, merges the changes to the main branch.&lt;/p&gt;

&lt;p&gt;If you are the owner or co-owner of a repository, you may not need to create pull requests to make changes as well as creating branches when working on a feature but its good practice so as to keep a record of changes made. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9.Merge your changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Click on merge pull request and confirm merge. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fffkjbwzy7qch6ddwzymj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fffkjbwzy7qch6ddwzymj.png" alt="merge pull request"&gt;&lt;/a&gt;&lt;br&gt;
Your changes will be merged to the main branch. You can delete the branch that you merged seeing as you no longer need it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdo0hwf9v8p37xjq7g5ud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdo0hwf9v8p37xjq7g5ud.png" alt="delete merged branches"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This also helps to make your work neater as too many branches can become messy. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10.Pull the changes to your local computer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you can recall our workflow so far, we: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;initialized a git repository locally &lt;/li&gt;
&lt;li&gt;added files and committed them &lt;/li&gt;
&lt;li&gt;created a new branch to work on &lt;/li&gt;
&lt;li&gt;created a GitHub repository and added it as a remote connection to our local repository &lt;/li&gt;
&lt;li&gt;pushed the new branch to the remote repository
&lt;/li&gt;
&lt;li&gt;created a pull request and merged the pull request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's then evident that our local repository is not the same as our remote and therefore to synchronize the changes made, in our terminal we run &lt;code&gt;git pull origin master&lt;/code&gt;. We can now &lt;code&gt;git status&lt;/code&gt; (to check which branch we are on to make sure we are on the main branch) then &lt;code&gt;git log&lt;/code&gt;to see all changes made&lt;/p&gt;

&lt;p&gt;In our next article, we will look into more advanced concepts such as going into the details of how git tracks changes to files.&lt;/p&gt;

&lt;p&gt;Adios&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>linux</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Getting Started with the Command Line Part 1: Intro to the Command Line</title>
      <dc:creator>mech2dev</dc:creator>
      <pubDate>Fri, 30 Sep 2022 04:24:15 +0000</pubDate>
      <link>https://dev.to/danielstai/getting-started-with-the-command-line-part-1-22bi</link>
      <guid>https://dev.to/danielstai/getting-started-with-the-command-line-part-1-22bi</guid>
      <description>&lt;p&gt;This article is part of a series of articles aimed at getting you, the complete beginner at using the command line, from zero to hero. &lt;/p&gt;

&lt;p&gt;At the end of this article you'll be able to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;define what the command line is&lt;/li&gt;
&lt;li&gt;advantages of using the command line &lt;/li&gt;
&lt;li&gt;various uses of the command line for both basic and advanced users &lt;/li&gt;
&lt;li&gt;getting started with basic commands&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article is based on Meta's Back-end Developer Certificate Course 3: Version Control. You can find a link to the course &lt;a href="https://www.coursera.org/learn/introduction-to-version-control"&gt;here&lt;/a&gt; and a link to my article on Course 1: Introduction to Back-end Development &lt;a href="https://dev.to/danielstai/introduction-to-backend-development-by-meta-a-review-59fm"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;We interact with computers with a daily basis to perform various tasks e.g. editing word documents, listen to music, watch videos, create files etc. At the heart of all these interactions is a process of the computer accepting data and giving some output. &lt;/p&gt;

&lt;p&gt;There are various ways we interact with the computer with the most popular one being through the use of graphical user interfaces (GUI). &lt;/p&gt;

&lt;p&gt;These are very popular since they are easy to use with little or no training but the have their own limitations including being slow and limiting full interaction with the computer. &lt;/p&gt;

&lt;p&gt;Another way to interact with the computer is the command line. Using the command line is faster, less prone to errors and offers a lot of flexibility.&lt;/p&gt;

&lt;p&gt;With the command line, we can perform some basic tasks like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creating directories&lt;/li&gt;
&lt;li&gt;creating files &lt;/li&gt;
&lt;li&gt;combining directories &lt;/li&gt;
&lt;li&gt;copying and moving files &lt;/li&gt;
&lt;li&gt;performing advanced searches&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of the more advanced uses of the command line include but not limited to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tracking software changes &lt;/li&gt;
&lt;li&gt;accessing remote servers &lt;/li&gt;
&lt;li&gt;unzipping archives &lt;/li&gt;
&lt;li&gt;accessing software manuals &lt;/li&gt;
&lt;li&gt;installing and uninstalling software &lt;/li&gt;
&lt;li&gt;checking, mounting and unmounting disks &lt;/li&gt;
&lt;li&gt;automating tasks with scripts &lt;/li&gt;
&lt;li&gt;controlling access to files and directories&lt;/li&gt;
&lt;li&gt;containerization (running and controlling self contained virtual software)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic Commands
&lt;/h2&gt;

&lt;p&gt;Let's introduce some of the most common and basic commands: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;cd&lt;/code&gt; - This command is used to change into a directory for example if you wanted to change into the desktop directory you would type &lt;code&gt;cd ~/Desktop&lt;/code&gt;. Similarly, to leave that directory you type &lt;code&gt;cd ..&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;touch&lt;/code&gt; command is used to create files. Say you want to create a file named example.html. This would be the correct command syntax for that - &lt;code&gt;touch example.html&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another basic command is the &lt;code&gt;mkdir&lt;/code&gt; command which is used to create a directory. If for instance you wanted to create a directory named project you could type &lt;code&gt;mkdir project&lt;/code&gt; into the command line&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So how do we put this all together to effectively communicate with the computer? Lets see this in a sample workflow:&lt;/p&gt;

&lt;p&gt;Lets say you want to create a folder for your new text project, create an empty text document,add some content into the text document and open the same on a code editor so as to work on it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;cd ~/Desktop&lt;/code&gt; - Change into Desktop directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mkdir myproject&lt;/code&gt; - Create a folder called myproject on the desktop&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd myproject&lt;/code&gt; - Change into the myproject folder&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;touch example.txt&lt;/code&gt; - Create a file called example.txt&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cat &amp;gt; example.txt&lt;/code&gt; - This will let us add some text to the file. After typing whichever text you want to go in the file, press CTRL+D to terminate the command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;code example.txt&lt;/code&gt; - Open example.html on VSCode to edit&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And there you go. We have defined what the command line is, its various uses and we've gone through a sample workflow using the command line to create a folder, create a file, add content to the file and finally open a code editor to continue working.&lt;/p&gt;

&lt;p&gt;On the next article in this series we will delve further into understanding the command line. We will look at how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create, rename and delete files and folders on your hard drive using Unix commands&lt;/li&gt;
&lt;li&gt;Use pipes and redirection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adios &lt;/p&gt;

</description>
      <category>linux</category>
      <category>git</category>
      <category>ubuntu</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
