Friday, April 5, 2024

Introduction to Node.js

This tutorial gives an introduction to Node.js, advantages of using Node.js and some features of Node.js.

Node.js is an open-source and cross-platform JavaScript runtime environment which gives you the capability to run JavaScript code on the server side.

JavaScript from browser to server

JavaScript is typically used in the browser to manipulate the DOM, which helps in making your web page dynamic.

Browsers have a JavaScript engine that parses and executes JavaScript code. For example, Firefox browser uses SpiderMonkey, Safari uses JavaScriptCore, Chrome browser uses V8 JavaScript engine to execute JavaScript in the browser.

Node.js, using the same V8 engine capabilities, allows you to run JavaScript code on the server-side. That makes Node.js one of the popular choices for developing web applications, RESTful APIs and microservices.

Node.js brings features to the JavaScript like working with the local file system, connecting to the DB which are not possible in the traditional JavaScript code running on the browser.

You can also create utility scripts using NodeJS, where you can create a JavaScript file and run that file with in the NodeJS runtime environment. Here is a simple example which uses 'fs' module to write content to the file using JavaScript.

helloworld.js

const fs = require('fs');
console.log('Hello from nodeJS');
fs.writeFileSync('Hello.txt', 'Hello from nodeJS');

You can go to the location where you have saved this file and run node helloworld.js to execute it. Successful execution should create a file Hello.txt at the same location where the code file resides with the given content.

Using JavaScript to develop server-side applications is a huge plus point in the favour of Node.js as the same frontend developers that write JavaScript for the browser can now write the server-side code without a steep learning curve of learning a completely different language.

Features of Node.js

Here are some of the features of Node.js.

1. Node.js is single-threaded

Node.js is single-threaded which means it serves requests without creating a new thread for each request. You may think that being single-threaded should make Node.js quite slow as no new threads are spawned to work concurrently. That is not true, by using a combination of non-blocking code, event loop and callbacks Node.js is capable of handling thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency.

2. Node.js uses non-blocking paradigm

As per Node.js documentation "libraries in Node.js are written using non-blocking paradigms, making blocking behaviour the exception rather than the norm."

When Node.js performs an I/O operation, like reading from the network, accessing a database or the filesystem it doesn't block the thread until the operation is finished wasting CPU cycles waiting. Node.js will resume the operations when the operation is finished.

Suppose you are doing a I/O operation like reading a file in a framework that uses blocking I/O in that case events may be as given below-

  1. Send the I/O request to the filesystem.
  2. Wait until the operation is finished.
  3. Once the operation is finished return the content of the file.
  4. Continue with the next request

Same I/O operation with Node.js which uses non-blocking I/O.

  • Send the I/O request to the filesystem.
  • Continue with the next request
  • When the operation is finished, a callback is executed to handle the result.

That's all for this topic Introduction to Node.js. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How to Install Node.js and NPM in Windows
  2. Creating HTTP server in Node.js
  3. How to Setup a Node.js Project
  4. JavaScript Arrow Function With Examples

You may also like-

  1. Bounded Type Parameter in Java Generics
  2. Nested Route (Child Route) in Angular
  3. React Declarative Approach
  4. Constructor chaining in Java
  5. Lambda Expression Examples in Java
  6. Java ThreadLocal Class With Examples
  7. Reading File in Java Using BufferedReader
  8. Bubble Sort Program in Python

No comments:

Post a Comment