<?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: Hardik Singh</title>
    <description>The latest articles on DEV Community by Hardik Singh (@fusionmaster7).</description>
    <link>https://dev.to/fusionmaster7</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%2F474007%2Fde6852a3-396c-4c46-8cd1-72115e407a31.jpeg</url>
      <title>DEV Community: Hardik Singh</title>
      <link>https://dev.to/fusionmaster7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fusionmaster7"/>
    <language>en</language>
    <item>
      <title>Setting up a basic Node Server</title>
      <dc:creator>Hardik Singh</dc:creator>
      <pubDate>Mon, 19 Oct 2020 06:38:30 +0000</pubDate>
      <link>https://dev.to/fusionmaster7/setting-up-a-basic-node-server-3ifa</link>
      <guid>https://dev.to/fusionmaster7/setting-up-a-basic-node-server-3ifa</guid>
      <description>&lt;p&gt;Node.js is a Javascript platform that is built on Chrome's V8 Javascript engine and allows you to build powerful applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Installing Node
&lt;/h2&gt;

&lt;p&gt;You can install node js by visiting &lt;a href="https://nodejs.org/en/download/"&gt;this link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Setting up Node
&lt;/h2&gt;

&lt;p&gt;After you have downloaded and installed node.js from the above link, it's time to set up the project directory!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an app directory&lt;/li&gt;
&lt;li&gt;Create a file named &lt;strong&gt;index.js&lt;/strong&gt; in that directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There you go! You have successfully set up a basic node.js project directory!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating the server
&lt;/h2&gt;

&lt;p&gt;We will be creating a basic HTTP server in our index.js file. For that, we need to first &lt;strong&gt;import&lt;/strong&gt; the &lt;strong&gt;http&lt;/strong&gt; module inside our file. To do that write the following line inside &lt;strong&gt;index.js&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const http = require("http);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next write the following lines of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http
  .createServer((req, res) =&amp;gt; {
    res.writeHead(200, { "Content-type": "text/plain" });
    res.end("Hello World\n");
  })
  .listen(3000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;createServer&lt;/strong&gt; function called above is used to create a basic HTTP server. We have passed a callback function to it as an argument. The callback function has two parameters: &lt;strong&gt;req&lt;/strong&gt; which represents the request part and &lt;strong&gt;res&lt;/strong&gt; which represents the response part.&lt;/p&gt;

&lt;p&gt;Inside the callback function, we write the following lines of code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The first line uses the &lt;strong&gt;res.writeHead&lt;/strong&gt; methods to set the response code and the header object for the response that our server is going to send. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The status code &lt;strong&gt;200&lt;/strong&gt; is used to denote that everything is 👍. You can read more about status codes &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The second parameter we pass is an object to denote the &lt;strong&gt;response header&lt;/strong&gt;. Here we are telling the browser the response received is of the type &lt;strong&gt;plain text&lt;/strong&gt;. Read more about content headers &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second line we use the &lt;strong&gt;res.end&lt;/strong&gt; function to send a response and end the &lt;strong&gt;request-response cycle&lt;/strong&gt;. Here we have returned the customary Hello World greeting.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;strong&gt;listen&lt;/strong&gt; function tells the server to listen for any requests on the port passed as the parameter. Here we pass 3000 as port.&lt;/p&gt;

&lt;p&gt;In the last line, we log something to the console so we know the server has started and is working.&lt;/p&gt;

&lt;p&gt;After this navigate to your directory and in the terminal type the following command  &lt;code&gt;node index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After this navigate to &lt;strong&gt;&lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt;&lt;/strong&gt; and voila! You should see &lt;strong&gt;Hello World&lt;/strong&gt; written in your browser.&lt;/p&gt;

&lt;p&gt;To stop the server use &lt;code&gt;Ctrl+c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And just like that, you have created your basic Node.js server! Stay tuned for more articles like this!&lt;/p&gt;

</description>
      <category>node</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
