<?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: Gerard Taylor (he/him)</title>
    <description>The latest articles on DEV Community by Gerard Taylor (he/him) (@thegerardtaylor).</description>
    <link>https://dev.to/thegerardtaylor</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%2F626877%2F00690e37-48cf-4c1e-b7a3-32c2433dfafa.png</url>
      <title>DEV Community: Gerard Taylor (he/him)</title>
      <link>https://dev.to/thegerardtaylor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thegerardtaylor"/>
    <language>en</language>
    <item>
      <title>Backend Camp: A Simple REST API</title>
      <dc:creator>Gerard Taylor (he/him)</dc:creator>
      <pubDate>Wed, 12 May 2021 15:14:28 +0000</pubDate>
      <link>https://dev.to/thegerardtaylor/backend-camp-a-simple-rest-api-54l4</link>
      <guid>https://dev.to/thegerardtaylor/backend-camp-a-simple-rest-api-54l4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you're coming from Frontend Web, iOS, or Android app development your knowledge of backend development might only be limited on how to query an API endpoint and magically receive the exact data you need. If you're exclusively a client (web, iOS, Android) developer then you might find that you don't really need to fully understand how the backend is implemented but it might help you to understand at a high level what happens when you dispatch a request to a backend API. That's the purpose of this article. We are going to be using &lt;a href="https://nodejs.org/en/"&gt;nodejs&lt;/a&gt;, &lt;a href="https://expressjs.com/"&gt;expressjs&lt;/a&gt;, and &lt;a href="https://www.npmjs.com/package/micronode-cli"&gt;micronode-cli&lt;/a&gt; to build a simple REST API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;In order to successfully complete this lesson you need to have the following software downloaded an installed on your computer:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.nodejs.org"&gt;nodejs&lt;/a&gt; - Runtime Environment to run Javascript applications outside of your browser&lt;/li&gt;
&lt;li&gt;Any code editor. We will be using &lt;a href="https://code.visualstudio.com/"&gt;VSCode&lt;/a&gt; in this lesson but any editor is fine.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.postman.com/"&gt;Postman&lt;/a&gt; - To send requests to our API without needing a browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step One: Installation
&lt;/h2&gt;

&lt;p&gt;Typically, when building a web server in nodejs, you'd need to manually install your dependencies which, at a minimum, usually consists of a &lt;strong&gt;Web Framework&lt;/strong&gt;. A web framework provides some higher-level abstractions on top of some web protocol to make development of web services easy and seamless. While you &lt;em&gt;can&lt;/em&gt; create a web server without using a framework, using battle tested software and web frameworks ensures that you are on the right track to building resilient and performant backend services. In this lesson, we are going to be using expressjs which is a web framework that makes it extremely simple to develop &lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer"&gt;REST (REpresentational State Transfer)&lt;/a&gt; APIs. The standard steps to create a backend API would be as follows:&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 // initialize your Node JS project
  $ npm install express // install the express js framework as a dependency
  $ touch server.js // create a server.js file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When creating multiple services, these steps can seem repetitive which is a perfect example of something that can be automated. The &lt;code&gt;micronode-cli&lt;/code&gt; application automates this process. We can install and run this script with the following commands:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install -g micronode-cli // installs micronode-cli globally on your system
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, in the directory of your choice, you can initialize (create) your service with the following commands:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mn create-service first-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You'll see the following output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Creating service named: first-service
Directory: [PATH]
Running npm init...
Node Project Initialized
Installing Dependencies...
Dependencies Installed:
    - express
Creating Dockerfile...
Initializing API...
Creating index.js ...
Dockerfile created.
API Created.
Server definition successfully created.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Both &lt;code&gt;mn&lt;/code&gt; and &lt;code&gt;micronode&lt;/code&gt; are valid versions of the command. In this case, we are using the shorthand &lt;code&gt;mn&lt;/code&gt;. &lt;code&gt;create-service&lt;/code&gt; is the action we want to run with the &lt;code&gt;mn&lt;/code&gt; cli tool. The &lt;code&gt;create-service&lt;/code&gt; command takes two arguments: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a service name (&lt;code&gt;first-service&lt;/code&gt; in this case) and &lt;/li&gt;
&lt;li&gt;An optional path to where to locate the service. The default is the current directory if left blank.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For reference, these are the arguments that are expected by &lt;code&gt;create-service&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mn create-service [SERVICE_NAME] -d [LOCATION]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After running the above command, you should have the following directory structure where you chose to install.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first-service/
    node_modules/
    api/
        api.js
    index.js
    Dockerfile
    package.json
    package-lock.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's talk about each of these files in turn.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt; - This is where the definition of your service resides. By naming your server file &lt;code&gt;index.js&lt;/code&gt; you can simply start your server by running: &lt;code&gt;$ node first-service&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dockerfile&lt;/code&gt; - This is a file that will allow you to create a docker image of your service for easy deployment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;package.json&lt;/code&gt; - This houses any configuration information related to your Node JS project. Each Node project has a package.json.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;api/api.js&lt;/code&gt; - This file houses all of the REST API Routes that your service uses. We'll get more into this later.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;node_modules&lt;/code&gt; - This is where all of your dependencies are installed.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/44297803/what-is-the-role-of-the-package-lock-json#:~:text=the%20npm%20docs:-,package-lock.,regardless%20of%20intermediate%20dependency%20updates."&gt;package-lock.json&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step Two: Running Our Server
&lt;/h2&gt;

&lt;p&gt;As stated above, we can run our server by running:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ node first-service&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see the following output&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;first-service Service is listening on Port: 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our service is now ready  to receive REST API requests on &lt;a href="https://en.wikipedia.org/wiki/Port_%28computer_networking%29"&gt;port&lt;/a&gt; 8000 on &lt;code&gt;localhost (i.e your computer)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step Three: Querying our Service
&lt;/h2&gt;

&lt;p&gt;Now that we have our service running, we can start sending requests to it. But how do we know which types of requests this service accepts? To understand this, we need to inspect our &lt;code&gt;index.js&lt;/code&gt; and our &lt;code&gt;api/api.js&lt;/code&gt; files. Let's take a look at the &lt;code&gt;api/api.js&lt;/code&gt; first.&lt;/p&gt;

&lt;h3&gt;
  
  
  api.js
&lt;/h3&gt;

&lt;p&gt;Your &lt;code&gt;api.js&lt;/code&gt; file should look something like this.&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="nx"&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="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Define Routes Here:&lt;/span&gt;

&lt;span class="c1"&gt;// CREATE&lt;/span&gt;
&lt;span class="nx"&gt;router&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="s2"&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;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;body&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;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;200&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="s2"&gt;Response from Post Request&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;// READ&lt;/span&gt;
&lt;span class="nx"&gt;router&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="s2"&gt;/:id&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="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;=&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;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;200&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="s2"&gt;Getting Data for: &lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;});&lt;/span&gt;



&lt;span class="c1"&gt;// UPDATE&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/:id&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="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;=&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;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;200&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="s2"&gt;Updating Data for: &lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// DELETE&lt;/span&gt;
&lt;span class="nx"&gt;router&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="s2"&gt;/:id&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="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;=&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;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;200&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="s2"&gt;Deleting data for: &lt;/span&gt;&lt;span class="dl"&gt;"&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="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;router&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;api.js&lt;/code&gt; file simply defines a set of &lt;strong&gt;routes&lt;/strong&gt; that are bound to certain &lt;strong&gt;request method types&lt;/strong&gt; (i.e GET, POST, PUT, DELETE). Each expressjs request method accepts two parameters. The first parameter is a string denoting the path (url) that this particular method should be bound to. The second parameter is the &lt;em&gt;function&lt;/em&gt; that should be executed when a request comes in. The &lt;em&gt;function&lt;/em&gt; parameters are a &lt;code&gt;req (Request)&lt;/code&gt; object and a &lt;code&gt;res (Response)&lt;/code&gt; object. The response object is what we send back to a client that is querying our API. Let's take a closer look at the &lt;em&gt;get&lt;/em&gt; request defined above.&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="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="s2"&gt;/:id&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="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;=&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;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;200&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="s2"&gt;Getting Data for: &lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;router.get("/:id", ...)&lt;/code&gt; is saying "define a &lt;em&gt;get&lt;/em&gt; route with the path "/" that expects a url parameter and name that parameter "id". You would query this url by saying "/10" and the ID in this case would be 10.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(req, res)&lt;/code&gt; are the anonymous function parameters. The expressjs documentation defines what is contained inside of the request and response objects. Generally speaking, the request object contains any information associated with a request (i.e parameters, query key-values, request body, headers, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const { id } = req.params;&lt;/code&gt; - This line is using destructuring which is javascript concept that allows us to pull out named object fields with the &lt;code&gt;{}&lt;/code&gt; syntax. The equivalent syntax without destucturing would be &lt;code&gt;const id = req.params.id;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res.status(200).send("Getting Data for: " + id);&lt;/code&gt; - We are using the &lt;code&gt;response (res)&lt;/code&gt; object to send a response back to the entity that sent a request to this endpoint. We do two things: 1. Set the response status code to OK (200), and we &lt;em&gt;send&lt;/em&gt; some data. In this case it's a simple string, but in a real-world setting this would be some complex JSON data structure. Now let's look at our &lt;code&gt;index.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  index.js
&lt;/h3&gt;

&lt;p&gt;Your &lt;code&gt;index.js&lt;/code&gt; should look something like this.&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="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;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="c1"&gt;// creates our application&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="c1"&gt;// parses the body of the requst into json&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;router&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./api/api&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// pull in the routes from our api.js file&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;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// pick a port to listen to.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serviceName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;first-service&lt;/span&gt;&lt;span class="dl"&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="s2"&gt;`/api/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;serviceName&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;router&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// the path that the API in api.js should be responsible for.&lt;/span&gt;

&lt;span class="c1"&gt;// start our server&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="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="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;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;serviceName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; Service is listening 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;The key difference with this file when compared to the &lt;code&gt;api/api.js&lt;/code&gt; file is that we are &lt;em&gt;not&lt;/em&gt; defining our API routes in the server file. Instead, we have placed them in a separate file and &lt;em&gt;imported (required)&lt;/em&gt; them in the server file. This is beneficial for maintainability and testing purposes and keeps our &lt;code&gt;index.js&lt;/code&gt; file lightweight and easy to understand. There are two important statements in this file: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;app.use("/api/first-service", router)&lt;/code&gt; - We have substituted the templated string for illustrative purposes but basically, our server will use the routes associated with the router (think &lt;code&gt;api/api.js&lt;/code&gt;) only when a request comes in with the path beginning with &lt;code&gt;/api/first-service&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app.listen&lt;/code&gt; is the final statement in this file and it tells our service to start up and listen to whatever port we have defined. In this case, it's &lt;code&gt;8000&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, to query our API, we can send a simple &lt;em&gt;get&lt;/em&gt; request to the following &lt;code&gt;route&lt;/code&gt; using our web browser.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8000/api/first-service/10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And your response should be the following single line of text:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Getting Data for: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;YAY! You've just built and queried your first API! Now, try to query some of the other endpoints using Postman!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this lesson, we created a simple REST API with very little code by using the &lt;code&gt;micronode-cli&lt;/code&gt;. In reality, you would use &lt;code&gt;micronode-cli&lt;/code&gt; to build a simple scaffold of your backend service, and then implement the &lt;em&gt;routes&lt;/em&gt; based on your particular use-case. &lt;code&gt;nodejs&lt;/code&gt; + &lt;code&gt;expressjs&lt;/code&gt; make it extremely easy to build and test backend services and I would highly recommend getting familiar with the express documentation going forward.&lt;/p&gt;

</description>
      <category>node</category>
      <category>rest</category>
      <category>express</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
