<?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: Monika Gole</title>
    <description>The latest articles on DEV Community by Monika Gole (@monikagole).</description>
    <link>https://dev.to/monikagole</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%2F1246473%2Fd256caea-ab76-4134-9fdb-0c77a95cbf4a.png</url>
      <title>DEV Community: Monika Gole</title>
      <link>https://dev.to/monikagole</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/monikagole"/>
    <language>en</language>
    <item>
      <title>Getting Started with Node.js: Building Your First Server</title>
      <dc:creator>Monika Gole</dc:creator>
      <pubDate>Tue, 02 Jan 2024 10:56:25 +0000</pubDate>
      <link>https://dev.to/monikagole/getting-started-with-nodejs-building-your-first-server-37jd</link>
      <guid>https://dev.to/monikagole/getting-started-with-nodejs-building-your-first-server-37jd</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Node.js has emerged as a powerful and versatile runtime for executing JavaScript on the server side. &lt;br&gt;
While JavaScript is traditionally associated with browser-based development, Node.js extends its capabilities to server-side scripting, enabling developers to build scalable and efficient server applications.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll embark on a journey to understand the fundamentals of Node.js by creating our first server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why Node.js?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Node.js is designed with a unique architecture that leverages an event-driven, non-blocking I/O model. This means that it can handle numerous simultaneous connections without the overhead of traditional thread-based servers. As a result, Node.js is well-suited for building real-time applications, APIs, and microservices where low-latency and high concurrency are crucial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Key Features of Node.js:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event-Driven Architecture:&lt;/strong&gt;&lt;br&gt;
Node.js uses an event loop to manage asynchronous operations, making it highly responsive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non-Blocking I/O:&lt;/strong&gt;&lt;br&gt;
Operations such as file I/O or network requests don't block the execution of other tasks, enhancing performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;npm Package Manager:&lt;/strong&gt;&lt;br&gt;
Node.js comes bundled with npm, a powerful package manager that simplifies the process of integrating third-party libraries and tools into your projects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Growing Popularity:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Node.js has gained widespread adoption in the development community, with major companies, including Netflix, LinkedIn, and PayPal, utilizing it to power their applications. &lt;br&gt;
Its versatility, speed, and a thriving ecosystem of packages make it a go-to choice for modern web development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Who is This Tutorial For?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This tutorial is aimed at developers who are new to Node.js or want to solidify their understanding by building a basic server. Whether you're a front-end developer looking to expand your skill set or a backend developer exploring new techn&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What We'll Cover:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
We'll start by installing Node.js and npm, the package manager that comes with it. Then, we'll walk through the process of creating a simple HTTP server using Node.js. By the end of this tutorial, you'll have a foundational understanding of Node.js and be ready to explore more advanced concepts in the exciting world of server-side JavaScript.&lt;/p&gt;

&lt;p&gt;Now, let's dive into the practical steps of installing Node.js and building our first server.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing Node.js
&lt;/h2&gt;

&lt;p&gt;Follow these steps to install Node.js on your machine:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Visit the Node.js Official Website:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Head to the &lt;a href="https://nodejs.org/"&gt;Node.js official website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The website will automatically detect your operating system and suggest the recommended version for download.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Download Node.js:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on the &lt;strong&gt;"Download"&lt;/strong&gt; button to start the download process.&lt;/li&gt;
&lt;li&gt;If you are using Windows, you may be prompted to choose between the LTS (Long Term Support) version and the Current version. For most users, the LTS version is recommended for stability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Install Node.js:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once the download is complete, run the installer.&lt;/li&gt;
&lt;li&gt;Follow the installation prompts, accepting the default settings in most cases.&lt;/li&gt;
&lt;li&gt;On Windows, you might need to restart your computer after the installation is complete.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Verify the Installation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open a terminal or command prompt.&lt;/li&gt;
&lt;li&gt;To verify that Node.js and npm (Node Package Manager) are installed, run the following commands:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node -v
npm -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;These commands should display the installed Node.js version and npm version, confirming a successful installation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Understanding npm:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm is a package manager for Node.js that simplifies the process of installing, updating, and managing third-party libraries or tools.&lt;/li&gt;
&lt;li&gt;To explore more about npm, you can visit the &lt;a href="https://www.npmjs.com/"&gt;npm official website&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Your First Node.js Server:
&lt;/h2&gt;

&lt;p&gt;Now that Node.js is installed, let's create a simple HTTP server using Node.js. Follow these steps to build your first server:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Install Visual Studio Code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you don't have Visual Studio Code installed, download and install it from the &lt;a href="https://code.visualstudio.com/download"&gt;official VS Code website&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Open VS Code and Create a New Project Folder:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open Visual Studio Code.&lt;/li&gt;
&lt;li&gt;Create a new folder for your Node.js project. You can do this through VS Code or using your terminal/command prompt.&lt;/li&gt;
&lt;li&gt;Open the terminal in VS Code by clicking on &lt;strong&gt;View &amp;gt; Terminal&lt;/strong&gt; or using the keyboard shortcut &lt;strong&gt;Ctrl +&lt;/strong&gt; .&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Initialize Your Project:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the terminal, navigate to your project folder (e.g., &lt;strong&gt;my-node-server&lt;/strong&gt;).
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd path/to/your/project/my-node-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Run the following command to initialize a new Node.js project. This will create a &lt;strong&gt;package.json&lt;/strong&gt; file.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Step 4: Create a Server Script:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In VS Code, create a new file named &lt;strong&gt;server.js&lt;/strong&gt; within your project folder.&lt;/li&gt;
&lt;li&gt;Open &lt;strong&gt;server.js&lt;/strong&gt; and add the following code to create a basic HTTP server:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const http = require('http');

const server = http.createServer((req, res) =&amp;gt; {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, Node.js Server!');
});

const PORT = 3000;
server.listen(PORT, () =&amp;gt; {
  console.log(`Server running at http://localhost:${PORT}/`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Save the file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Understanding the Server Code:&lt;/strong&gt;&lt;br&gt;
Let's break down the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We import the built-in &lt;strong&gt;http&lt;/strong&gt; module, which provides functionality for creating an HTTP server.&lt;/li&gt;
&lt;li&gt;Using &lt;strong&gt;http.createServer&lt;/strong&gt;, we create a server instance that listens for incoming requests.&lt;/li&gt;
&lt;li&gt;The callback function inside **createServer **handles each incoming request, setting the response status and sending a simple text message.&lt;/li&gt;
&lt;li&gt;We define the port number (e.g., 3000) on which the server will listen.&lt;/li&gt;
&lt;li&gt;Finally, we call &lt;strong&gt;server.listen&lt;/strong&gt; to start the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Run Your Server:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the terminal, ensure you are in the project folder.&lt;/li&gt;
&lt;li&gt;Run the following command to start your server:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;You should see the console message indicating that the server is running at &lt;a href="http://localhost:3000/"&gt;http://localhost:3000/&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 7: Test Your Server:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your web browser and navigate to &lt;a href="http://localhost:3000/"&gt;http://localhost:3000/&lt;/a&gt;. You should see the message "Hello, Node.js Server!".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Congratulations! You've Built Your First Node.js Server in Visual Studio Code&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You've successfully created and run a basic Node.js server using Visual Studio Code. &lt;br&gt;
This setup provides a convenient environment for Node.js development and can be extended for more complex projects.&lt;/p&gt;
&lt;h2&gt;
  
  
  Navigating the Node.js Ecosystem:
&lt;/h2&gt;

&lt;p&gt;Now that you have a running Node.js server, let's explore the rich Node.js ecosystem and learn how to navigate it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Understanding npm:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm (Node Package Manager) is a crucial tool in the Node.js ecosystem. It simplifies the process of managing dependencies, installing packages, and handling project dependencies efficiently.&lt;/li&gt;
&lt;li&gt;Run the following command in your terminal to see the currently installed packages in your project:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;This command will display a tree structure of installed packages and their versions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Installing Packages:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
You can use npm to install third-party packages for your Node.js projects. For example, let's install the &lt;strong&gt;express&lt;/strong&gt; framework, a popular choice for building web applications:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command adds &lt;strong&gt;express&lt;/strong&gt; as a dependency in your &lt;strong&gt;package.json&lt;/strong&gt; file and installs it in the &lt;strong&gt;node_modules&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Exploring the npm Registry:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit the &lt;a href="https://www.npmjs.com/"&gt;npm Registry&lt;/a&gt; to discover a vast collection of Node.js packages and modules.&lt;/li&gt;
&lt;li&gt;You can search for packages, explore their documentation, and learn how to use them in your projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You've installed Node.js and npm, gaining a fundamental understanding of setting up a Node.js project.&lt;/li&gt;
&lt;li&gt;Created a basic HTTP server using Node.js, showcasing the event-driven nature of Node.js and its simplicity in handling HTTP requests.&lt;/li&gt;
&lt;li&gt;Explored the Node.js ecosystem by understanding npm and installing a third-party package (express).&lt;/li&gt;
&lt;/ul&gt;

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