<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: jesse-crypted</title>
    <description>The latest articles on Forem by jesse-crypted (@jessecrypted).</description>
    <link>https://forem.com/jessecrypted</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%2F901780%2F78660246-84e7-486c-b33d-4f205bd9cc15.jpeg</url>
      <title>Forem: jesse-crypted</title>
      <link>https://forem.com/jessecrypted</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jessecrypted"/>
    <language>en</language>
    <item>
      <title>I created a web server 👨‍💻</title>
      <dc:creator>jesse-crypted</dc:creator>
      <pubDate>Thu, 04 Aug 2022 13:40:00 +0000</pubDate>
      <link>https://forem.com/jessecrypted/i-created-a-web-server-4dc0</link>
      <guid>https://forem.com/jessecrypted/i-created-a-web-server-4dc0</guid>
      <description>&lt;p&gt;On day 2 of my 100 days of code in learning Node.js, I was able to learn ho to create a web server without using the express framework. So the web server was built by using the Nodejs HTTP module.&lt;br&gt;
So let dive in the tutorial.&lt;br&gt;
&lt;strong&gt;What is a web server and how come Node.js can do that:&lt;/strong&gt; A web server can either be a software or hardware that responds to client (i.e browser, it is through a browser we access a web server) request. Web servers store or host websites, content on the internet and when users needed them they make a request to the server. The reason Node.js can do this is because with Node.js we can now run Javascript outside the browser.&lt;br&gt;
So for us to build this server we use a module provided by Node.js called the http module, we initialise it when we start our code. We can save our code in a file called app.js&lt;br&gt;
&lt;code&gt;const http = require("http");&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;http&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;http&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&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;writeHead&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="p"&gt;{&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;content-type&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="s1"&gt;text/html&lt;/span&gt;&lt;span class="dl"&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;h1&amp;gt;Hello from the server&amp;lt;/h1&amp;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;server&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="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;127.0.0.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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...listening to requests on port 8000&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 can now run our web server using node app.js. Visit &lt;a href="http://127.0.0.1:8000"&gt;http://127.0.0.1:8000&lt;/a&gt; and you will see a message saying "Hello from the server".&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: this web server is however hosted on a localhost with an IP Address of &lt;code&gt;127.0.0.1&lt;/code&gt; and on port &lt;code&gt;8000&lt;/code&gt;. &lt;br&gt;
&lt;strong&gt;IP address&lt;/strong&gt; are special adresses for computers on the internet.&lt;br&gt;
&lt;strong&gt;Port numbers&lt;/strong&gt; are like doors into a computer. &lt;br&gt;
Yay we have finally created our web server 🎉🎉🎉🎉🎉😉&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>100daysofcode</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
