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


What is Node js?

Leave a Comment

Node is Open source and cross platform run time environment for executing JavaScript code outside of a browser.


We generally use node to build back-end service also called API or application programming interfaces.


These are the service that used by frontend application or client applications. For example, mobile app running in mobile phones or web app running inside of web browser. These client apps are simply what the user sees and interacts with. They are just a surface, they need to talk to some service on the web server to store data or send emails, push some notification and so on.





Node is ideal for building highly-scalable, data-intensive and real time back-end service that power our client application


But there us also more application to use as back-end service. So, what's so special about node??


What's so special about node??

Node is easy to get started and can be used for prototyping and agile development but it can also be used for building super-fast and highly scalable service


it's used in production by large companies such as paypal, uber, netflix and so on


IN paypal they re-build there entire java and spring based application using node and found that the node application was build twice as fast with fewer people in 33% fewer lines of code 40% fewer files and more importantly they doubled the number of request served per second while decreasing the average response time by 35%, so node js is an excellent choice for building highly scalable services.


Another reason for using node, is that in node applications we use JavaScript, so if you are front end developer and know JavaScript , you can reuse your JavaScript skill and transition to a full stack developer and get as better job with better pay you don't have to learn a new programming language. also because you can use JavaScript both on front end and on the back end your source code will be cleaner and more consistent so you would use the same naming convention, the same tools and same best practices.


Finally another reason for using node is it has the largest ecosystem of open source libraries available to you so for pretty much any features or building blocks you want to add your application there is some free open sourced library out there that you can use so you don't have to build these building blocks from scratch and instated you can focus on the core of your application.

Powered by Blogger.