Export to Excel in Node js

Leave a Comment

To export data in node js we will use exceljs module. So let's direct jump into the code. 

Let's first install this module using below command 

npm i exceljs

now create file name as exportToExcel.js and copy paste below code

const excel = require("exceljs");

function exportExcel() {
    let workbook = new excel.Workbook();
    let worksheet = workbook.addWorksheet("Straw Hat");

    let exportData = [];

    exportData.push({
        id: "1",
        name: "Monkey D. Luffy",
        designation: "Captain",
        description: "The Captain of the Straw Hat Pirates"
    }, {
        id: "2",
        name: "Roronoa zoro",
        designation: "Swordsmen",
        description: "The Swordsmen of the Straw Hat Pirates"
    }, {
        id: "3",
        name: "Vinsmoke Sanji",
        designation: "Cook",
        description: "The cook of the Straw Hat Pirates"
    })

    worksheet.columns = [
        { header: "Id", key: "id", width: 25 },
        { header: "Name", key: "name", width: 25 },
        { header: "Designation", key: "designation", width: 25 },
        { header: "Description", key: "description", width: 25 },
    ];

    worksheet.addRows(exportData);

    workbook.xlsx.writeFile('One_Piece.xlsx');

}

exportExcel()

It's simple code first we import excel js in our code, then let's create workbook variable and add worksheet as "Straw Hat". We can add multiple worksheet in one excel file for different different data.


After that let's create array of data which we want to export in excel. after that let's add the column in worksheet with mapping of data, header value as column name and id value as data key name which we have to map to that column name. 


worksheet.addRows() function takes array of data as parameter and create worksheet with data and finally we will use workbook write file function to create a file with excel file name parameter. 


When we run this file node exportToExcel.js then it will automatically create the One_Piece.xlsx in folder with above json data.







How node works

Leave a Comment

So as we know node js application are highly-scalable and this is because of the non-blocking or async nature of node js.


What do I mean by async nature of node js??

 

Asynchronous programming is a design pattern which ensures the non-blocking code execution

Non blocking code do not prevent the execution of piece of code. In general if we execute in Synchronous manner i.e one after another we unnecessarily stop the execution of those code which is not depended on the one you are executing this is how application build with framework like ASP.net and rails works out of the box.


Asynchronous does exactly opposite, asynchronous code executes without having any dependency and no order. This improves the system efficiency and throughput.


So when we receive a request on the server a thread is allocated to handle that request. And suppose in that request we have to fetch data form database as we all know sometime querying a DB takes a time.


When the database is executing the query that thread will be wait for the result and imaging what would happen if we have a large number of concurrent client at some point we are going to run out of threads for new client have to wait until threads are free. If we don’t want then to wait then we have to add more threads to server which more hardware and cost. 


So this is the problem with blocking or sync architecture and as I explained that how’s application build with framework like asp.net work by default. We can also have async architecture in asp.net but we have to extra work for that.

But Node js application are async by default so we don’t have to do anything extra. In node we have a single thread to handle all request when request arrives that single thread is used to handel that request if we need to query a DB our thread doesn’t have to wait for the db to return the data. While the db is executing our query the thread used to serve another request. 


Once DB ready with result it puts a message in what we call an event queue. Node is consistently monitoring this event queue in the background. When it find an event in this queue it will take it out and process it. This kind of architecture makes node js ideal for building applications that include a lot of disk or network access. We can serve more client without the need to throw in more hardware. And that why node application are highly scalable. Node should not be used in CPU-intensive apps such as a video encoding service. Because while executing these operations, other clients have to wait for the single thread to finish its job and be ready to serve them. Node should only be used for data intensive and real time application


What is node js architecture

Leave a Comment

 In last blog we learnt that what is node js ?, Node is runtime environment. 


But what is run time environment really??

 

Well, before node we used JavaScript only to build application that run inside as browser, so every browser having JavaScript engine that takes JavaScript code and convert into machine code or code which computer can understand and execute the code but what are the engine which used by browser

Below is the list of browsers with their JS engine

  • Microsoft edge uses chakra.
  • Firefox uses Spider monkey.
  • Chrome uses V8.


So due to variety of these engine some time JavaScript code behave differently in one browser and another browser. Browser provide runtime environment for JavaScript code.


document.getElementById('')
 

For e.g. in browser we have windows or document object these object allows us to work with environment in which code is running.


Up to 2009 JavaScript code was running In browser, In 2009 Ryal dahl creator of node came up with brilliant idea. He thought let’s execute JavaScript outside the browser so he took google V8 engine which is the fastest JavaScript engine and embedded in C++ program called that programming node. 


So similar to browser node is runtime environment for JavaScript code. It contains the JavaScript engine that can execute JavaScript code but it also have certain object that provide and environment for a JavaScript code but these object are different form environment object having a browser. 


For e.g. In node js we don’t have document object instead of that we have other object which gives more interesting capability. For e.g. we can work with file system or listing for request on given port and so on. We can’t do stuff like this on browser.


So  basically node is program that include JavaScript v8 engine plus some additional module that give us capability not available inside the browser we can work with the file system or the network and so on.


Chrome and node share the same JavaScript engine but they provide diff runtime environment for java script.


Node is not an programming language, Node is also not an framework it’s runtime environment for executing JavaScript code.

 

So don’t compare node js with C# and ASP.net


Powered by Blogger.