Published on

How to Get Started with Node.js: A Beginner's Guide

TABLE OF CONTENTS

Introduction

Node.js is a free, open-source, and cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside the browser, typically on a server. It enables you to build fast, scalable web applications, real-time services like chat apps, and tools that handle a large number of simultaneous requests efficiently. Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight and efficient — perfect for data-intensive applications that need to run in real-time. It also comes with npm (Node Package Manager), providing access to thousands of libraries, making development easier and faster.

Prerequisites

To follow this beginner's guide to Node.js, you should have basic knowledge of JavaScript, including functions, loops, and variables. Since Node.js handles tasks asynchronously, it's important to understand promises and callbacks. You should also be comfortable using the command line to run Node.js commands and manage files. Finally, basic knowledge of HTTP and APIs will help as we interact with servers.


Step 1: Install Node.js

Follow these steps to install Node.js on your machine:

  1. Go to the official Node.js website: Node.js Downloads.
  2. Choose your version: Select either the LTS (Long Term Support) version or the Current version. The LTS version is recommended for beginners.
  3. Download and install: Download the installer and follow the on-screen instructions to complete the installation.

To verify the installation, open a terminal and run:

node -v

Step 2: Set Up Your Environment

Once Node.js is installed, it’s time to set up your development environment:

  1. Create a new project directory:
mkdir my-nodejs-app
cd my-nodejs-app
  1. Initialize a new Node.js project:.
npm init -y

This will create a package.json file, which holds metadata about your project.

Step 3: Write a "Hello World" Application

Now that your environment is ready, let's write your first Node.js app:

  1. Create a new file named app.js:
touch app.js
  1. Add the following code to app.js:
// Import the built-in http module to create an HTTP server
const http = require("http");
 
// Create an HTTP server
const server = http.createServer((req, res) => {
  // Set the response status code to 200 (OK)
  res.statusCode = 200;
 
  // Set the response header to indicate the content type is plain text
  res.setHeader("Content-Type", "text/plain");
 
  // Send the response body and close the connection
  res.end("Hello World!\n");
});
 
// Define the port number where the server will listen for incoming requests
const PORT = 3000;
 
// Make the server listen on the specified port
server.listen(PORT, () => {
  // Log a message to the console when the server is running
  console.log(`Server running at http://localhost:${PORT}/`);
});

Step 4: Run Your Application

To run your Node.js server:

  1. Start the server by running the following command:
node app.js
  1. Open a browser and navigate to http://localhost:3000. You should see "Hello World!" displayed in your browser.

Conclusion

Congratulations! You've successfully installed Node.js, set up your environment, and created your first "Hello World" Node.js application. From here, you can expand on this knowledge to build more complex applications.

Resources

Frequently Asked Questions (FAQs)