<?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: Israr khan</title>
    <description>The latest articles on DEV Community by Israr khan (@israromar).</description>
    <link>https://dev.to/israromar</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%2F311243%2Fd3f173bb-a37a-49ca-9601-7946018a2c38.jpg</url>
      <title>DEV Community: Israr khan</title>
      <link>https://dev.to/israromar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/israromar"/>
    <language>en</language>
    <item>
      <title>How to create a simple NodeJS HTTP server?</title>
      <dc:creator>Israr khan</dc:creator>
      <pubDate>Sat, 03 Sep 2022 16:05:43 +0000</pubDate>
      <link>https://dev.to/israromar/how-to-create-a-simple-nodejs-http-server-4ea8</link>
      <guid>https://dev.to/israromar/how-to-create-a-simple-nodejs-http-server-4ea8</guid>
      <description>&lt;h2&gt;
  
  
  Step 1: Install Node.js
&lt;/h2&gt;

&lt;p&gt;We are assuming you have Node.js installed on your machine. If you haven't installed it, click on the following link and install it simply.&lt;br&gt;
&lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;NodeJS&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Initialize the project
&lt;/h2&gt;

&lt;p&gt;Let's start; create an empty directory and initialize your project by running the following command.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydsvppa61wfvmilfxnd4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydsvppa61wfvmilfxnd4.png" alt="code snippet" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next step is to create an empty JavaScript file named index.js and install &lt;a href="https://www.npmjs.com/package/express" rel="noopener noreferrer"&gt;Express.&lt;/a&gt;&lt;br&gt;
Express is a minimal and easy-to-learn framework for Node.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 3: Starting the server
&lt;/h2&gt;

&lt;p&gt;Now that everything is ready, it's time to code our actual API.&lt;/p&gt;

&lt;p&gt;Let's start the server first. &lt;/p&gt;

&lt;p&gt;In the below code snippet, we are initializing our &lt;code&gt;app&lt;/code&gt; and starting the local server at port 3000.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;node index.js&lt;/code&gt; to start the server.&lt;/p&gt;

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

&lt;p&gt;For more context, listen() function is used to establish the connection on specified host and port.&lt;/p&gt;

&lt;p&gt;It takes two params:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first is the port number or host.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second (optional) is a callback function that runs after listening to a specified host or value.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moving forward, let's try to access "localhost:3000" in the browser and see what we are getting.&lt;/p&gt;

&lt;p&gt;We are getting a "404 Not Found" response which is correct as we haven't defined any endpoints yet.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Step 4: Create Endpoints
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;get&lt;/code&gt; method allows us to create HTTP GET requests.&lt;/p&gt;

&lt;p&gt;It accepts two params:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first is path/route.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second is a callback function that handles the request to the specified route.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The callback function itself accepts two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first is the request which is the data to the server.&lt;/li&gt;
&lt;li&gt;The second is the response which is the data to the client.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Suppose you want to display all the users whenever the client requests the "/users" endpoint.&lt;/p&gt;

&lt;p&gt;To return the data from the server to the client, we have the &lt;code&gt;send&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;In the code snippet below, we send an array of objects with &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt; fields.&lt;/p&gt;

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

&lt;p&gt;Perfect!&lt;/p&gt;

&lt;p&gt;Let's restart the server by running the &lt;code&gt;node index.js&lt;/code&gt; command and see what we are getting.&lt;/p&gt;

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

&lt;p&gt;You can make as many endpoints as you want using the same technique.&lt;/p&gt;

&lt;p&gt;For demonstration purposes, let's quickly create a POST request endpoint.&lt;/p&gt;

&lt;p&gt;As POST request is to create or add new data to the server. We first need to add middleware so that we can parse the JSON body.&lt;/p&gt;

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

&lt;p&gt;We are defining a POST endpoint at the "/user/3" route.&lt;/p&gt;

&lt;p&gt;We implemented the logic of throwing a "400 Bad Request" status code if the user forgets to pass the name value in the request body.&lt;/p&gt;

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

&lt;p&gt;Let's try to access this endpoint now.&lt;/p&gt;

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

&lt;p&gt;As you can see, we are getting a response. 🎉&lt;/p&gt;

&lt;p&gt;Great! You just built a REST API.&lt;/p&gt;

&lt;p&gt;The DevRel team at @RapidApi originally wrote this thread.&lt;/p&gt;

&lt;p&gt;Credits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/" rel="noopener noreferrer"&gt;@Rapid_API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/Prathkum" rel="noopener noreferrer"&gt;@Prathkum&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow them for more exciting content.&lt;/p&gt;

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