<?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: Ann Mukami</title>
    <description>The latest articles on DEV Community by Ann Mukami (@kiunga1).</description>
    <link>https://dev.to/kiunga1</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%2F1357641%2Feeb1aa32-ff8f-4440-88c6-a8c1653e3a78.png</url>
      <title>DEV Community: Ann Mukami</title>
      <link>https://dev.to/kiunga1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kiunga1"/>
    <language>en</language>
    <item>
      <title>Building a Simple Web Server in Node.js: A Beginner's Guide</title>
      <dc:creator>Ann Mukami</dc:creator>
      <pubDate>Mon, 06 May 2024 13:34:30 +0000</pubDate>
      <link>https://dev.to/kiunga1/building-a-simple-web-server-in-nodejs-a-beginners-guide-i8d</link>
      <guid>https://dev.to/kiunga1/building-a-simple-web-server-in-nodejs-a-beginners-guide-i8d</guid>
      <description>&lt;p&gt;Are you curious about how websites work behind the scenes? Have you ever wondered what happens when you type a URL into your browser and hit enter? In this beginner-friendly guide, we'll explore the basics of building a simple web server using Node.js, a popular JavaScript runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up the Tools&lt;/strong&gt;&lt;br&gt;
Before we dive into building our web server, let's gather the tools we'll need:&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');
const fs = require('fs');
const url = require('url');
const querystring = require('querystring');
const figlet = require('figlet');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Creating a Server#
&lt;/h1&gt;

&lt;p&gt;Now, let's create our web server using Node.js's built-in &lt;code&gt;http&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const server = http.createServer((req, res) =&amp;gt; {
  // Server code here
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code sets up a server that listens for incoming requests and defines what to do when a request is received.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reading Requests#
&lt;/h1&gt;

&lt;p&gt;When a request comes in, we need to understand what the requester is asking for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const page = url.parse(req.url).pathname;
const params = querystring.parse(url.parse(req.url).query);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're parsing the URL to extract the path and any query parameters that might be included in the request.&lt;/p&gt;

&lt;h1&gt;
  
  
  Checking What's Asked For#
&lt;/h1&gt;

&lt;p&gt;Based on what the requester wants, we respond accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (page == '/') {
  // Send main page
} else if (page == '/otherpage') {
  // Send other page
} else if (page == '/api') {
  // Deal with API request
} else {
  // Handle 404 error
}

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

&lt;/div&gt;



&lt;p&gt;We check the path of the request and take appropriate actions, such as serving different pages or handling API requests. If the requested page doesn't exist, we send a "404 Not Found" error message.&lt;/p&gt;

&lt;h1&gt;
  
  
  Sending Back the Response
&lt;/h1&gt;

&lt;p&gt;Once we know what the requester wants, we send back the response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We set the appropriate HTTP status code (e.g., 200 for "OK") and send the requested data back to the requester.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dealing with Different Requests#
&lt;/h1&gt;

&lt;p&gt;Sometimes, the requester may ask for something we don't have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;figlet('404!!', function(err, data) {
  res.write(data);
  res.end();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we use the figlet module to create an ASCII art representation of "404!!" and send it back as an error message.&lt;/p&gt;

&lt;h1&gt;
  
  
  Listening All the Time#
&lt;/h1&gt;

&lt;p&gt;Finally, we need our server to listen for requests continuously:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.listen(8000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line of code tells our server to listen on port 8000 for incoming requests, making our web server always available.&lt;/p&gt;

&lt;p&gt;And there you have it! With just a few lines of code, we've created a simple web server in Node.js. While this example is basic, it provides a foundation for understanding how web servers work and can serve as a starting point for more complex applications. Happy coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
