Page Contents
Node.js Tutorial
Node.js tutorial provides basic and advanced concepts of Node.js. Our Node.js tutorial is designed for beginners and professionals both.
Node.js is a cross-platform environment and library for running JavaScript applications which is used to create networking and server-side applications.
Our Node.js tutorial includes all topics of Node.js such as Node.js installation on windows and linux, REPL, package manager, callbacks, event loop, os, path, query string, cryptography, debugger, URL, DNS, Net, UDP, process, child processes, buffers, streams, file systems, global objects, web modules etc. There are also given Node.js interview questions to help you better understand the Node.js technology.
What is Node.js
Node.js is a cross-platform runtime environment and library for running JavaScript applications outside the browser. It is used for creating server-side and networking web applications. It is open source and free to use. It can be downloaded from this link https://nodejs.org/en/
Many of the basic modules of Node.js are written in JavaScript. Node.js is mostly used to run real-time server applications.
The definition given by its official documentation is as follows:
Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Node.js also provides a rich library of various JavaScript modules to simplify the development of web applications.
- Node.js = Runtime Environment + JavaScript Library
Different parts of Node.js
The following diagram specifies some important parts of Node.js:
Features of Node.js
Following is a list of some important features of Node.js that makes it the first choice of software architects.
- Extremely fast: Node.js is built on Google Chrome’s V8 JavaScript Engine, so its library is very fast in code execution.
- I/O is Asynchronous and Event Driven: All APIs of Node.js library are asynchronous i.e. non-blocking. So a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call. It is also a reason that it is very fast.
- Single threaded: Node.js follows a single threaded model with event looping.
- Highly Scalable: Node.js is highly scalable because event mechanism helps the server to respond in a non-blocking way.
- No buffering: Node.js cuts down the overall processing time while uploading audio and video files. Node.js applications never buffer any data. These applications simply output the data in chunks.
- Open source: Node.js has an open source community which has produced many excellent modules to add additional capabilities to Node.js applications.
- License: Node.js is released under the MIT license.
Install Node.js on Windows
13 Nov 2024 | 1 min readTo install and setup an environment for Node.js, you need the following two softwares available on your computer:
- Text Editor.
- Node.js Binary installable
Text Editor:
The text editor is used to type your program. For example: Notepad is used in Windows, vim or vi can be used on Windows as well as Linux or UNIX. The name and version of the text editor can be different from operating system to operating system.
The files created with text editor are called source files and contain program source code. The source files for Node.js programs are typically named with the extension “.js”.
The Node.js Runtime:
The source code written in source file is simply JavaScript. It is interpreted and executed by the Node.js interpreter.
How to download Node.js:
You can download the latest version of Node.js installable archive file from https://nodejs.org/en/
Here, you deploy the installation of node-v4.4.2 LTS recommended for most users.
Accept the terms of license agreement.
Choose the location where you want to install.
Ready to install:
Install Node.js on Linux/Ubuntu/CentOS
We can easily install Node.js on linux/ubuntu/centOS/fedora/linuxmint etc. To install Node.js on Linux (Ubuntu) operating system, follow these instructions:
1) Open Ubuntu Terminal (You can use shortcut keys (Ctrl+Alt+T).
2) Type command sudo apt-get install python-software-properties
3) Press Enter (If you have set a password for your system then it will ask for the password)
4) Type the password and press enter
5) Type command sudo apt-add-repository ppa:chris-lea/node.js
6) Press Enter
7) Again Press Enter to continue
8) Type command sudo apt-get update ( Wait for sometime)
9) Type command sudo apt-get install nodejs npm
10) Type command sudo apt-get install nodejs
Installation completed. Now you can check the version of Node by node –version
Check the version of npm by npm -v
Now you can check the node.js in your installed program list by typing this command
dpkg –get-selections
Node.js First Example
There can be console-based and web-based node.js applications.
Node.js console-based Example
File: console_example1.js
- console.log(‘Hello JavaTpoint’);
Open Node.js command prompt and run the following code:
- node console_example1.js
Here, console.log() function displays message on console.
Node.js web-based Example
A node.js web application contains the following three parts:
- Import required modules: The first step is to use require directive to load http module and store returned HTTP instance into http variable. For example:
1. var http = require(“http”); - Create server: You have to establish a server which will listen to client’s request similar to Apache HTTP Server.
- Read request and return response: Server created in the second step will read HTTP request made by client which can be a browser or console and return the response.
How to create node.js web applications
Follow these steps:
- var http = require(“http”);
- http.createServer(function (request, response) {
- // Send the HTTP header
- // HTTP Status: 200 : OK
- // Content Type: text/plain
- response.writeHead(200, {‘Content-Type’: ‘text/plain’});
- // Send the response body as “Hello World”
- response.end(‘Hello World\n’);
- }).listen(8081);
- // Console will print the message
- console.log(‘Server running at http://127.0.0.1:8081/’);
File: main.js
- var http = require(“http”);
- http.createServer(function (request, response) {
- // Send the HTTP header
- // HTTP Status: 200 : OK
- // Content Type: text/plain
- response.writeHead(200, {‘Content-Type’: ‘text/plain’});
- // Send the response body as “Hello World”
- response.end(‘Hello World\n’);
- }).listen(8081);
- // Console will print the message
- console.log(‘Server running at http://127.0.0.1:8081/’);
- Import required module: The first step is to use ?require? directive to load http module and store returned HTTP instance into http variable. For example:
- Create server: In the second step, you have to use created http instance and call http.createServer() method to create server instance and then bind it at port 8081 using listen method associated with server instance. Pass it a function with request and response parameters and write the sample implementation to return “Hello World”. For example:
- Combine step1 and step2 together in a file named “main.js”.
How to start your server:
Go to start menu and click on the Node.js command prompt.
Now command prompt is open:
Set path: Here we have save “main.js” file on the desktop.
So type cd desktop on the command prompt. After that execute the main.js to start the server as follows:
- node main.js
Now server is started.
Make a request to Node.js server:
Open http://127.0.0.1:8081/ in any browser. You will see the following result.
Now, if you make any changes in the “main.js” file, you need to again run the “node main.js” command.