Node.js Tutorial for Beginners

Node.js is an open-source, cross-platform runtime environment that enables developers to build server-side applications with JavaScript. It is built on Chrome’s V8 JavaScript engine, which makes it fast and efficient. Node.js has gained immense popularity in recent years due to its versatility and ease of use. In this tutorial, we will walk you through the basics of Node.js and how to get started with building your first application.

Setting up Node.js

Before we dive into coding, we need to set up Node.js on our system. Node.js can be downloaded from its official website, and it comes with an installer that makes the installation process a breeze. Once you have downloaded the installer, run it and follow the instructions.

Once the installation is complete, you can check if Node.js is installed correctly by opening your terminal or command prompt and typing the following command:

node -v

This command will print the version of Node.js that is currently installed on your system.

Creating your first Node.js application

Now that you have Node.js set up, let’s create our first application. Open your favorite code editor and create a new file called “app.js”. In this file, we will create a simple “Hello, World!” application.

arduino
// app.js

console.log("Hello, World!");

Save the file and then open your terminal or command prompt, navigate to the directory where you saved the “app.js” file, and type the following command:

node app.js

This command will run the “app.js” file, and you should see the message “Hello, World!” printed to the terminal.

Understanding Node.js modules

One of the most powerful features of Node.js is its module system. Node.js comes with a set of built-in modules, and you can also create your own modules.

To use a module in your application, you need to import it using the “require” function. For example, to use the “fs” module to read and write files, you would use the following code:

javascript
const fs = require("fs");

This code imports the “fs” module and assigns it to the “fs” variable.

Creating a web server with Node.js

Now that you understand the basics of Node.js and its module system, let’s create a simple web server using the built-in “http” module.

javascript
// server.js

const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.write("Hello, World!");
  res.end();
});

server.listen(3000, () => {
  console.log("Server is listening on port 3000");
});

Save the file as “server.js” and run it using the following command:

node server.js

This command will start the server, and you should see the message “Server is listening on port 3000” printed to the terminal. Now open your browser and navigate to “http://localhost:3000“, and you should see the message “Hello, World!” displayed in the browser.

Conclusion

In this tutorial, we covered the basics of Node.js and how to get started with building your first application. We covered setting up Node.js, creating a simple “Hello, World!” application, understanding Node.js modules, and creating a web server with Node.js. Node.js is a powerful and versatile platform, and with the knowledge you have gained from this tutorial, you can start building your own applications.

0368826868