Posts

Showing posts from June, 2023

Understanding Node.js Event Emitter: A Powerful Event Handling Mechanism

Image
Introduction Event-driven programming is a popular paradigm that allows programs to respond to events or triggers asynchronously. In Node.js, the EventEmitter class from the core events module provides a powerful mechanism for implementing event-driven programming. This article will explore the EventEmitter class, its features, and how to utilize it effectively in your Node.js applications. What is the EventEmitter? The EventEmitter class is a fundamental part of Node.js and serves as the backbone for event handling. It enables you to define and emit custom events, as well as register event handlers to respond to those events. The EventEmitter follows the observer pattern, where objects (event emitters) maintain a list of listeners (event handlers) and notify them when an event occurs. Getting Started To begin working with the EventEmitter , first, ensure that you have Node.js installed on your system. The events module, including the EventEmitter class, comes bundled with Node.js

Convert JSON to CSV with Node.js using readFileSync

Image
  I have to convert some JSON format files to CSV format to read or access them properly from MS-Excel. You are able to see I split my VS-code screen and on the left side, you can see the code that I have used for conversion, and on the right part, you will get the JSON file that we want to convert it. In the terminal below, you can view the logs as “CSV file created successfully”. So let’s go step-by-step to get the same result as mine. There are a few pre-requisites, they are as follows: a. Nodejs installed on your local machine —   https://nodejs.org/ Install packages “ json2csv”  using the following command: $  npm install json2csv npm install json2csv Additionally, you don’t need to install any packages specifically for the built-in fs (file-system) module since it is a part of Node.js and comes pre-installed with it. You can directly use it like this as shown below: const json2csv = require ( 'json2csv' ). parse ; const fs = require ( 'fs' ); The first line impo