1. Asynchronous and Event Driven: All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. Example:

var fs = require(“fs”);

fs.readFile(‘input.txt’, function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});

console.log(“Program Ended”);

2. Single-Threaded but Highly Scalable: Node.js uses a single-threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests. Example:

// Import events module
var events = require(‘events’);

// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

// Create an event handler as follows
var connectHandler = function connected() {
console.log(‘connection succesful.’);

// Fire the data_received event
eventEmitter.emit(‘data_received’);
}

// Bind the connection event with the handler
eventEmitter.on(‘connection’, connectHandler);

// Bind the data_received event with the anonymous function
eventEmitter.on(‘data_received’, function(){
console.log(‘data received succesfully.’);
});

// Fire the connection event
eventEmitter.emit(‘connection’);

console.log(“Program Ended.”);

3. Fast in Code Execution: Node.js library is very fast in code execution. This is due to the fact that Node.js applications are written in JavaScript, and JavaScript is a very fast scripting language. Example:

// Function to calculate the sum of two numbers
function add(a, b) {
return a + b;
}

// Print the sum
console.log(add(1,2));

4. No Buffering: Node.js applications never buffer any data. These applications simply output the data in chunks. Example:

var http = require(‘http’);

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.write(‘Hello World!’);
res.end();
}).listen(8080);

Leave a Reply

Your email address will not be published. Required fields are marked *