Node-js
Top 25 Node.js Interview Questions & Answers
Download PDF 1) What is node.js? Node.js is a Server side scripting which is used to build...
In this tutorial, you will learn
Node makes extensive use of streams as a data transfer mechanism.
For example, when you output anything to the console using the console.log function, you are actually using a stream to send the data to the console.
Node.js also has the ability to stream data from files so that they can be read and written appropriately. We will now look at an example of how we can use streams to read and write from files. We need to follow the below-mentioned steps for this example
Step 1) Create a file called data.txt which has the below data. Let assume this file is stored on the D drive of our local machine.
Tutorial on Node.js
Introduction
Events
Generators
Data Connectivity
Using Jasmine
Step 2) Write the relevant code which will make use of streams to read data from the file.
var fs = require("fs");
var stream;
stream = fs.createReadStream("D://data.txt");
stream.on("data", function(data) {
var chunk = data.toString();
console.log(chunk);
});
Code Explanation:-
Output:
Writing to a file
In the same way, that we create a read stream, we can also create a write stream to write data to a file. Let's first create an empty file with no contents called data.txt. Let's assume this file is placed in the D drive of our computer.
The below code shows how we can write data to the file.
var fs = require("fs");
var stream;
stream = fs.createWriteStream("D://data.txt");
stream.write("Tutorial on Node.js")
stream.write("Introduction")
stream.write("Events")
stream.write("Generators")
stream.write("Data Connectivity")
stream.write("Using Jasmine")
Code Explanation:-
If you open the data.txt file, you will now see the following data in the file
Tutorial on Node.js
Introduction
Events
Generators
Data Connectivity
Using Jasmine
Within Node applications, streams can be piped together using the pipe() method, which takes two arguments:
A typical example of using pipes, if you want to transfer data from one file to the other.
So let's see an example of how we can transfer data from one file to the other using pipes.
Step 1) Create a file called datainput.txt which has the below data. Let assume this file is stored on the D drive of our local machine.
Tutorial on Node.js
Introduction
Events
Generators
Data Connectivity
Using Jasmine
Step 2) Create a blank empty file called dataOutput.txt and placed it on the D drive of your local machine.
Step 3) Write the below code to carry out the transfer of data from the datainput.txt file to the dataOutput.txt file.
var fs = require("fs");
var readStream = fs.createReadStream("D://datainput.txt");
var writeStream = fs.createWriteStream("D://dataOutput.txt");
readStream.pipe(writeStream);
Code Explanation:-
If you now open the dataOutput.txt file, you will see all the data which was present in the datainput.txt file.
Events are one of the key concepts in Node.js and sometimes Node.js is referred to as an Event-driven framework.
Basically, an event is something that happens. For example, if a connection is established to a database, then the database connection event is triggered. Event driven programming is to create functions that will be triggered when specific events are triggered.
Let's look at a basic example of defining an event in Node.js.
We are going to create an event called 'data_received'. When this event is triggered, the text "data received" will be sent to the console.
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('data_received', function() {
console.log('data received succesfully.');
});
eventEmitter.emit('data_received');
Code Explanation:-
When the program is run, the text "data received" will be sent to the console as shown below.
When defining events, there are different methods for events which can be invoked. This topic focuses on looking at each one of them in detail.
Sometimes you may be interested in reacting to an event only the first time it occurs. In these situations, you can use the once() method.
Let's see how we can make use of the once method for event handlers.
Code Explanation:-
If the code is executed properly, the output in the log will be 'data_received successfully'. This message will only appear once in the console.
At any point in its lifetime, an event emitter can have zero or more listeners attached to it. The listeners for each event type can be inspected in several ways.
If you are interested in only determining the number of attached listeners, then look no further than the EventEmitter.listenerCount() method.
(Note: Listeners are important because the main program should know if listeners are being added on the fly to an event, else the program will malfunction because additional listeners will get called.)
Code Explanation:-
If the code is executed properly, the value of 2 will be shown in the console log.
Each time a new event handler is registered, the event emitter emits a newListener event. This event is used to detect new event handlers. You typically use newListener event when you need to allocate resources or perform some action for each new event handler.
var events = require('events');
var eventEmitter = events.EventEmitter;
var emitter = new eventEmitter();
emitter.on("newListener", function(eventName, listener) {
console.log("Added listener for " + eventName + " events");
});
emitter.on('data_received', function() {});
emitter.on('data_received', function() {});
Code Explanation:-
If the above code is executed properly, the below text will be shown in the console. It just shows that the 'newListener' event handler was triggered twice.
Added listener for data_received events
Added listener for data_received events
Download PDF 1) What is node.js? Node.js is a Server side scripting which is used to build...
Mostly all modern-day web applications have some sort of data storage system at the backend. For...
A module in Node.js is a logical encapsulation of code in a single unit. It's always a good...
What is GraphQL? GraphQL is an application layer server-side technology which is developed by...
To start building your Node.js applications, the first step is the installation of the node.js...
What is GraphQL? GraphQL is an application layer server-side technology which is developed by...