Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is built on Chrome’s V8 JavaScript engine, which is known for its speed and efficiency. Node.js enables developers to write server-side applications in JavaScript, making it a popular choice for building scalable and efficient web applications.
In this tutorial, we will cover the basics of Node.js, including how to install it, create a simple server, and work with modules and packages.
Installing Node.js
To get started with Node.js, you first need to download and install it on your machine. You can download Node.js from the official website (https://nodejs.org/). Choose the appropriate version for your operating system and follow the installation instructions.
Once Node.js is installed, you can check if it’s installed correctly by running the following command in your terminal:
node -v
This command will display the version of Node.js that is currently installed on your machine.
Creating a Simple Server
One of the most common use cases for Node.js is building web servers. To create a simple server, follow these steps:
- Create a new file called
server.js
. - Open the file in a text editor and add the following code:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
- Save the file and run the following command in your terminal to start the server:
node server.js
- Open a web browser and navigate to
http://127.0.0.1:3000/
. You should see the message "Hello, World!" displayed in the browser.
In this example, we created a simple HTTP server using the http
module that comes built-in with Node.js. The server listens on port 3000 and responds with a "Hello, World!" message to any incoming requests.
Working with Modules and Packages
Node.js has a module system that allows you to split your code into reusable and manageable modules. You can create your own modules and use modules provided by the Node.js core or third-party packages.
To use a module in Node.js, you need to use the require
function. For example, to use the fs
(File System) module to read a file, you can do the following:
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
In this code snippet, we are using the fs
module to read the contents of a file called example.txt
. The readFile
function takes three arguments: the file path, the encoding (in this case, UTF-8), and a callback function that is called with the file contents once it has been read.
Node.js also has a package manager called npm (Node Package Manager) that allows you to easily install and manage third-party packages. To install a package, you can run the following command in your terminal:
npm install package-name
For example, to install the popular Express framework for building web applications, you can run:
npm install express
Once the package is installed, you can require it in your code and use it as a module. For example, to create a simple Express server, you can do the following:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server running at http://127.0.0.1:3000/');
});
In this example, we are using the Express package to create a web server that listens on port 3000 and responds with a "Hello, Express!" message when a GET request is made to the root route (‘/’).
Conclusion
Node.js is a powerful and versatile platform for building web applications and server-side code in JavaScript. In this tutorial, we covered the basics of Node.js, including how to install it, create a simple server, and work with modules and packages. With Node.js, you can build fast, scalable, and efficient applications for a wide range of use cases.