Node.js - Top 30 Interview Questions and Answers for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends if you are an aspiring or already a software developer then you definitely aware about M or mean stack where an stands for node node is getting so popular that as I can see in one article it has the maximum number of jobs in the market for backend that's why I created this mini interview course for node if you are completely new to node you will still be able to understand all the questions because I have aligned the questions in a step by step learning order from 0 to 30 like super 30 and if you are experienced then revise these questions at least once because most of the questions are like the basic concepts the fundamentals only for example this one node is single threaded how then this one how node is a synchronous programming how it does then how events event emits event cues work and node applications these are very basics in interviews if you do not answer the advanced question that's okay but if you can't answer the initial or basic fundamental questions that's humilating right like a assassination and you know the result then right and sometimes we are working and project knowledge we have but somehow we forgot those Basics so let's revise all right I have divided these questions into four chapters so let's start with the first chapter questions here in this chapter we are covering very basic questions about node even if you know these questions still revise them at least once because sometimes we know only the half information and sometimes even the incorrect information we have you will get more pointers after watching these questions so let's start with the first question and yes before starting just like And subscribe the channel because soon I will be launching the top 200 interview questions on node which will be like a complete guide for the node interviews what is nodejs now most of us know two things for sure first we have programming languages like python Java JavaScript C for writing the code second we have Frameworks corresponding to these languages for example Jango framework for python spring for Java Express for JavaScript and net for C of course there can be more than one framework for a single language right now what is the role of framework the role of framework is to simplify the creation of large applications how by providing many inbuilt and reusable libraries classes functions Etc okay now as you can see nodejs is not here right that means nodejs is neither a language nor a framework so what is nodejs nodejs is a runtime environment for executing JavaScript library now listen carefully a runtime environment is an environment that executes the program for example cpython is the runtime environment for Jango framework to run execute the python then jvm is for the spring framework then CLR is for NET Framework right so basically runtime environment is responsible for things like memory management and converting the high level language like Java JavaScript C to lower level level languages lower languages like machine language which your system can understand okay so that is the job of runtime environment now Frameworks are like a wrapper over the runtime environment okay so Frameworks basically provide additional features over the runtime environment all right uh I think you know now what is runtime environment and it's a good start and here is the definition of nodejs for later vision from the book node is neither a language nor a frame not a framework node or nodejs same thing basically is a runtime environment for executing the JavaScript code on the server side why server side not client side that I will answer in upcoming question how know is a runtime environment on the server side and what is V8 basically so let's quickly understand this see we have JavaScript language now JavaScript language will run and will be executed by the browsers like Chrome and others that we already know how browsers run JavaScript basically every browser has a JavaScript engine inside it like Chrome has the V8 JavaScript engine Edge has chakra Firefox has spider monkey so inside Chrome browser V8 is the JavaScript engine which runs the JavaScript code now an engineer ran uh used the same V8 JavaScript engine and created nodejs as a wrapper over it okay therefore now with nodejs runtime environment JavaScript can run outside the browser also okay outside the browser and as browser execute the JavaScript on client side now we have the nodejs to execute the JavaScript script on server side when I say client side it means browser okay it's related to browser and when I say server side it's basically the back end okay great that's the whole idea and same is written here and a very simple and easy VA definition is also written here for later revision from the book what is the difference between a framework and a runtime environment I already covered this but I covering it separately this question so that you do not miss it when you are revising okay so here are the languages their runtime environments and then their Frameworks remember one language can have more than one runtime environment and can have more than one Frameworks okay so it depends actually and here is the definition of a runtime environment the runtime environment focuses on providing the necessary infr structure for the code execution and including some basic services like memory management and input output operations okay that is runtime environment framework primarily focuses on simplifying the development process by offering struct structured set of tools libraries best practices reusable classes and functions okay so framework is like a wrapper over the runtime environment to make the application development process more and more simpler for the developers that's the answer to this question hi friends here is a quick notification there is a full node interview course with 200 questions and this PP and PDF from which I'm teaching is available there for the download the link is in the description check it out in case if you do not want to miss anything for your interviews many students were able to crack the interviews via my course and I'm sure you will also do it all right and yes that is coming soon okay so all right now let's continue again with our next question what is the difference between nodejs and express.js again a very short answer question you already already know this diagram right and the difference is here nodejs is a runtime environment that allows the execution of JavaScript code on server side whereas express.js is a framework built on the top of the nodejs it's like a wrapper over the nodejs why was it built or created this express.js the answer is here uh it is designed to simplify the process of building web applications and apis by providing you know simple routing system middleware support Etc to simplify okay so basically by using node plus Express building server side apis will be very very easy and fast that's it what are the differences between client side and server side very important question for understanding the complete flow from the client to the server let's quickly explore so suppose you are a user and you open any website in your browser by entering the URL now one request will go to the UI server which is mostly created in angular react or any other uii technology okay now for some simple operations like image download that can be and the operations which can be performed on the existing data which is already there in your UI like sorting filtering that can be done these T kind of operation can be done by the UI server itself and the UI server send the HTML and JavaScript as a response to the user browser okay but for some complex operations like fetching more data or different data for that the client has to send the request to the back end or API server and that server again written in the JavaScript language but this time with the nodejs run time okay then our node API will fetch the data from the database get the response and send the response with the data back to the UI so the UI will basically finally display that data to you now here the browser and node will both execute JavaScript right but there are different es between the way the browser will execute and the node will execute so let's see first the browser runs on the userb browser whereas node runs on the server nothing is special second difference is HTML CSS and JavaScript are languages in the browser whereas JavaScript is the language in node next difference is document window navig and event objects are present in the browser but they are not there in the node so for example this document. get element by ID so that kind of things you cannot do in node because nodejs of course does not need any interaction with the direct interaction with the UI right next differen is like uh request response server and data these object database objects are present in node but no not in browser so of course these are important objects for handling the request and response on the server side so browser don't have much to do with it okay last difference is browser responsibility is to handle the UI display interaction of the users and the client side logic whereas the server side or node handles business logic like data storage access authentication authorization Etc Great so these are some differences between how the client side execute the JavaScript and the JavaScript in the server side node all right now we have the second chapter of node which is very very important but before that one thing from my own experience about interviews the interview journey is not easy okay it's really very hard but do remember doing easy things will not bring success doing uncomfortable and difficult things will only bring success that's the logic why after like 3 4 years of college degree and 3 years of working experience that will not give you that much but 3 months of difficult interview Journey can can can completely change your career okay so one selection and you will forgive forget all your rejections great coming back to this chapter and watching it you will never be confused about the single threading multi-threading synchronous asynchronous Concepts okay so let's start with the first question what are the seven main features of node this is a very very very important questions we already have many Technologies for creating the web applications like java.net Etc right then why why node is the best choice now that is because of the nodes features only right so here are the seven features node like JavaScript is a single threaded the program second is programing node executes in a asynchronous way then node applications are even driven then node uses V8 JavaScript engine which you already know then uh node is crossplatform and therefore it feature can run on can any operating system right then node has npm node package manager which manage its all its dependencies finally node has real time capabilities meaning it is very good for handling B directional communication B Direction communication like chat application gaming application where user basically interact in real time right great now in upcoming question I will explain all the features one by one and uh this I will show the and I will also show the advantages and I will show how those advantages are basically coming from these features only okay so that we will see in upcoming questions what is single threaded programming let's quickly understand we already know first user from the browser send the request to the client and then to the server site right now inside the server suppose the request has to perform five independent task for that in the case of normal programming a single thread will be created like this then first This Thread T1 will start start the task one now listen carefully thread T1 now will wait until task one is completed okay T1 is waiting once task one is completed T1 will go to the task two and start it and then again thread T1 will wait until the task two is completed then same for task three three and finally the completion for task four the and after completion like for after the task four the thread T1 will inform the API about the completion of all the task now this is simple straightforward single threaded programming and it's a synchronous programming because here tasks are executing one by one and one task is waiting for for the previous tasks to be completed right but in node or JavaScript we can achieve a synchronous programming even with the single thread and that I will explain you in the upcoming question what is synchronous programming now many have confusions between synchronous and single thread programming for example here is the single threaded execution which I previously explained uh remember single threaded programming means we are talking about single thread okay a single thread is responsible for starting the task now with a single thread we can achieve both synchronous and asynchronous operations okay here by using the single thread we are doing synchronous operations because synchronous programming means each task should be performed one after the other okay that that's what we are doing here right so that is synchronous programming the disadvantage of this synchronous programming approach is performance this synchronous approach will take more time see uh a total like it's 10 then 8 4 5 total 27 seconds will be taken to execute all the task and one more problem is there the synchronous approach is a blocking approach because here until task one is completed task two has to wait right so task one is like a blocker for the task two so that is the blocking thing now in order to perform the task concurrently for improving the time performance and for a non-blocking pro uh approach we will see the asynchronous approach uh approach programming in the upcoming question by using what yes node node okay which which actually happened actually in node great so finally here I have written the simplest definition for the synchronous programming which I already explained and also the difference between synchronous and single thread programming for later revision purpose what is multi-threaded programming let's quickly understand multi-threaded programming so that we know the difference between single thread and multi-threaded so here the user and client side send the request to the server you already know then the server will first create a thread T1 which will start this task one okay now listen carefully in a multi-threaded approach when one thread starts one task and it then it sees another task after that then immediately another new thread T2 will be created and that T2 will start the task two okay so while the T1 thread is waiting for task one to be completed in parallel the new thread T2 will start Task two and therefore both tasks are being executed in parallel similarly when T2 start Task two then for the next next task three another T3 is created which will start Task three and then T4 thread is cre created for task 4 finally you can see how different tasks have different threads in a multi-threaded environment this approach is very good for CPU intensive tasks where many tasks has to be performed parallel if you remember our Cod computer processor and it has multiple codes right so having more codes means they are much better for multi-threaded programming applic appications okay for heavy desktop software it is very good this multi-threading approach but for internet apis this multi-threaded approach can be a problem because multiple threads can create deadlock problems if not properly handled that's why we prefer a single threaded asynchron asynchronous approach for JavaScript and node applications what is a synchronous programming now this is our main t Target and this is what we will use in our node applications previously we saw how synchronous programming is not good for performance and also it's a blocking approach that's also something we will solve here in nodejs suppose a request has to complete four tasks okay like this then first a single thread is created which will initiate the task one okay now listen carefully now this thread T1 will not wait for this T this task one to be completed will not wait okay instead after initiating task one this thread T1 will immediately jump onto the to initiate or start the next task two then after initiating task two T1 jumps on to initiate task three and then task four great now you have the most important point about single threaded asynchronous operations and the point is that our single thread T1 is just initiating the task and not waiting for its completion right so now you can imagine how in this case our tasks are running concurrently uh and if you remember in synchronous way the total time was 10 + 8 + 5 plus 4 + 5 right uh 27 seconds total which at which is the least time but now the total time taken can be 10 seconds in the best case but time will can be something more it depends on your Hardware also okay but in any case it will be better than the synchronous operation so that was we solved the first problem the time and performance problem another problem was the blocking issue so here Task 1 2 3 4 while executing are not blocking the execution of the next task okay so when task one is executing task two in parallel concurrently can also execute okay so therefore it's a non-blocking way the synchronous approach all right that's all about asynchronous programming now you might ask one more question like when task one is executing independently then this thread T1 is busy in starting the other tasks right it is doing its work so how the system will know when the task one is finished so there comes the role of the events in node whenever any task is completed or finished it will raise an event okay so then that event will immediately listened or heard by our thread T1 and our thread will immediately stop whatever it is doing at that point of time okay and what T1 will do on priority basis it will immediately report the task completion to the source and then again T1 will resume whatever it was doing earlier so that's why we also call nodejs uh a as an has an asynchronous programming approach and also the event driven architecture okay in upcoming question I will discuss more about events before that here are some steps for the execution of a synchronous operations using a single thread in a node which I already explained writing it down for quick re revision later from the book and here is the simplest and most complete definition of a synchronous in node a synchronous flow can be achieved by its single threaded non-blocking and even driven architecture remember in JavaScript also by using the asyn AIT or promises you can achieve this asynchronous programming okay what is the difference between synchronous and asynchronous programming I already explained them separately but here I have also written down the differences uh it's Point by point the and you can see I have also pointed out the advantages of the asynchronous programming over the synchronous programming so if you understand synchronous as synchronous programming then these difference and advantages are very easy to understand you can either pause the pause the video and read it or later you can revise the differences from the book that's the quick answer of this question what are events event I event Q event Loop and event driven in note all right this question will clear up all your Concepts about the events in node first of all what is an event an event is like a signal that something has happened in a program like a notification like a action has happened like if you remember we have those click events in the UI right so now these events are notification must have some Source from where they are these events are originated right so these sources are called event emitters uh who generate the events like this event one and event two all generated events are then stored in this in a q okay which we call what event Q in node okay clear until now then node will pick one event at a time and the event handler or we can also say event listener will perform the code as per the event requirement so basically here you will write the code that how you want to handle the event in your application okay so it's nothing like event handler is nothing but it's a function okay so written by us okay in case of threads it is written by the node to how to handle it asynchronously inside the node rent time environment okay all right after that the the next event will be picked by the event handler from the event queue and the it goes on like next event so and so on and so on so this the picking up of this events like this one by one from the event Q in order in in the same order is called the event Loop okay so great that's the whole story easy right about how the events are originated and how they they processed and also here I have the written the simplest and the most easy to understand definitions of uh events event emitter event cues event handlers and event Loops okay all I already explained and finally we have this event driven architecture event driven architecture is a combination of all of the above what does it means it means operation in node are driven by or based on the events only okay driven by events so that's why we call node as the even driven has the even driven architecture great that's the answer to this question what are the main features and advantages of node so in interviews many times we directly started speaking about the advantages of node without relating them with the features for a great answer you should know know that from where the advantages are coming okay every advantage in any technology comes from some of the from some features of that technology right so therefore if we are able to relate both features and advant Advantage then it's a great answer and you will also feel it after listening the this answer here are the five features you already know right now uh nodejs has asynchronous operation it has a Google Chrome V8 JS engine it has event driven architecture it's a crossplatform and it uses the JavaScript language okay so these are the features now here are the advantage we get from these features first because of asynchronous nodejs can enable handling multiple concurrent requests and can do non-blocking execution of threads right then second because of node is built it is built in V8 JavaScript engine from Google Chrome therefore node just execute code very fast very quickly okay JavaScript engine only executes your JavaScript code right next Advantage is because node is even driven architecture therefore it is very efficient in handling asynchronous operations and events okay handling events is will be very easy uh because threats will respond as per the events generated if you remember the synchronous operations right also in the chat applications gaming applications why events are important because there are too many events generated when by directional Communication in chat applications so that will be very easy and then node will be a great choice okay next is uh cross PL platform uh it supports deployment on various operating systems like Windows Linux and which basically enhancing the flexibility of the your application so that it can be hosted in any operating system then next Advantage is as we code in the JavaScript language only in node so basically there is no need to learn a new programming language okay so that's also an advantage same language can be used for the client side and for the server side development also right then last and because of all of the above feature node is suitable for building scalable applications that can handle increased loads SC scalable means so suppose if tomorrow when your application will grow after 2 year 3 year it will become bigger and bigger and then if it is developed in node your application then it will be very easy to grow it to scale it and add more features to it so that is a scalability all right that's the whole story of the features and the advantages of node what are the disadvantages of node when to use or when not to use node so everything of course has some disadvantages also in this world so does the node also have something you already know when to use node for your project right it's ideal for realtime applications like chat application online gaming collaborative tools because of its event driven architecture then also it is excellent for building lightweight and ESC scalable rest apis that handles a large number of concurrent requests or connections okay then also it's a great choice for microservices based architecture because it have the modular and escap label system so modular approach is there in the node also now the main question here is when not to use node so you should not use node in project that demand demand CPU intensive task for example video processing for example some software which require heavy Al algorithms to be executed which required multi- threading so because in that case multi-threaded technologies will be a great choice not a single thread technology like node okay because multi- thread can perform multiple task multiple core they can use the heavy servers and that so that's the answer of this question you should not use node for simpu intensive task congratulations on completing the last chapter where there is a will there is a way believe me or ask any experienced person interviews are the best source of learning and the best source of earning also in my 15 years career I have tried certifications Learning Without goal just learning a new technology and then forgetting but posting resume and preparing for interviews is the best best best learning phase because then I can't cheat I can't SK skip I can't assume the concept cepts I must face the reality all right now we will have these questions about project setup and most importantly modules here we will see questions on modules how they interact and what are the types of modules so let's start with the first question of this chapter how to set up a node project uh this is not an interview question but if you are new to node then just do these seven steps very quickly I will show you so if you already know then skip this question okay no problem so first download nodejs from the internet and install it second download VSS code or any code editor and again install it then create a new folder uh anywhere in your system like my first project a folder okay then open vs code which you installed and open this my first project folder in it then in vs code open that terminal and run this command npm init y okay hyphen y so this then create a new Js file appjs like this in your project folder okay so this is the your first appjs file now you can run your project nodejs application by using this command node app.js so great so this is your project is ready and uh on the left you can add more files as per your application and here you can see the file view at the bottom you have the terminal now to see this terminal you have to click this three dots above and then View and terminal option is there actually at the top and then you can write the commands here in the terminal okay that is the purpose of terminal for executing the commands great I know it was very quick and it was not in detail but yes because this is more focused on the interviews not on the setup right this is not an interview question what is npm what is the role of node modules folder in your project if you expand this node modules folder then here you can see the libraries the dependencies that can be used in your node project okay these dependencies will make your development easy and simple so if you delete any of these dependencies then might be some functionalities of your node project will not work stop working okay so never touch this folder great what are node modules then the node modules folder contains all the dependencies of the node project and what is npm npm is the node package manager that manages this node modules folder okay or you can say manage these dependencies for for your node uh project that is npn what is the role of package.json file in node when you create your project for the first time you will find this package Json file in the in your project this package Json file contains the project metadata metadata means the information about your project for example here you can see the information about this project project name Project work project description project author project license okay so this is the information about this project right and the same thing I have written here also for later reference again metadata is just the information like a book is there so content in inside but the name of the book the author of the book this is the information the metad data about the book okay what are modules in node what is the difference between a function and a module so not in just node but in every other technology module also means a piece of functionality okay what it is a functionality for example in an e-commerce e-commerce application like Amazon orders products payments these are different modules so what is a module here is the definition a modules contains a specific functionality that can be easily reused within a nodejs application second point is ideally in node a JavaScript one Javascript file can be treated as a single module so here this my module 1js is a one module is one module which contains this say hello function now the question is okay so what is the difference between a function and a module see module is is a concept for example order is a module in an e-commerce application but inside the order module there can be one function or even more than one function multiple functions like in order we have get order list submit order and post order right so that is the difference okay and here I have written a module is a broader concept that encapsulate the functionality while a function is a specific spefic set of instructions uh statements within that module and yes modules can contain multiple functions and variables great in short you can say that module is a feature which is implemented by using functions in node project normally we create JavaScript files so like this one Javascript file represent one module but yes you can put more than one module in a single Javascript file but that is not a recommended approach you should never do it okay so great that's the answer to this question how many ways are there to export a module now inside a module we mostly have functions like this but we can also have variables or objects inside modules okay now by default these functions variables objects are available only inside this same Javascript file and they are not accessible outside this file okay by default now to make them accessible outside in other files we have to export them for that one way is to use modules. exports object like this module. exports do function name equal to function name now same for exporting variable and objects also you can do the same this is one way another direct way direct way of exporting is like this this is direct here we are directly using the export object to export the function variable function variable or any object right directly exporting there is no need to write separate export statements in this case and this is like a shortcut approach which is very famous nowadays and the same is written above them for later revision from the book a very short and simple question what will happen if you do not export the module you know the answer if you do not export the module then the module function will not be available outside the module okay simple how to import single or multiple functions from a module now normally we have only one function inside a module like this and we can export that function like this as well module. exports equal to function name this is the shortcut way okay it is for just one function remember but if you have multiple function then you should use the usual approach the module. exports. function name equal to function a like this only okay so again this is the shortcut synex now in order to import these functions in other files first you should use the require function which is an inbuilt function inside the node that is already there inside the node to help you then to import a single function you can use the module name and pass the parameter required by the function okay directly so there is no need to use the function name here uh node will automatically understand and detect because you have only one function there in your module from where you are importing right but if you have more than one function then we should use import the fun uh function like this module. function name and then pass the function parameter so basically we have to then provide the function name because we have more than one function that's the answer for importing the functions and same is written here what is module wrapper function now what actually happen if you create a Javascript appjs file in your node project like this and you will run it then it will give you the proper output like this right but how here we do not have any function so how it is executing without the function okay so actually internally what node will do it will convert this code or you can say wrap this fjs code inside a function like this so now now the same code you can see the wrapped inside the function okay automatically by node you do not have to do this and this function will be be executed finally so this wrapper function is obviously automatically generated by the node you need not to write this so the why why why you need not to write this the purpose is basically to make things simpler and shorter for the developers so that's the answer and the simplest and easiest uh definition is here in nodejs each module is wrapped in a function which we call the module wrap function be before it is executed right what are the types of modules in node we have three types of modules in NES first is built-in modules or we also call them core modules these are already present modules in the nodejs that provides essential functionalities common functionalities for example file system HTTP server path manipulation utilities so these are provide modules are provided by the node we we have to just import them and use them and we can call their functions okay second type of modules are local modules these are the user defined modules which as a developer we have to develop like develop uh developers have to develop as per their project requirements okay so then we can create these modules we can export their functions these modules so that other files in our project we can also use there also okay last are third party modules these are like external packages or libraries created by the community outside basically for providing the ex uh additional functionalities for your node projects now to use them you have to first install them okay because they are not already present in your node and then you can install by npm install commands and like this npm install loes this lades is a external package okay third party module and then you can use them in your application great these are the three types of module and all I have explained is written here for later revision from the book great job and if you are doing all the hard work then you are on the right track remember one thing and I always repeat it again and again interview skill is the most important skill in the it or software industry if you have the interview cracking skill then you are not scared of any recession any bad appraisal switching jobs or anything else but if you don't have this skill then maybe you study very hard work very hard in your job in your company but still still you will have that insecurity deep down inside you okay the point is to learn this skill and be ready for any situation all right coming back to this chapter now we will see the question on the top built-in modules and this is a very important because you will use these built-in modules in many places in your node project so let's start with the first question of this chapter what are the top five five buil-in modules commonly used in node projects now there are many buil-in modules in node but the most famous and most you used modules are these FS module path module OS module events and HTTP modules I will quickly cover the role and the importance of these modules in the upcoming questions explain the role of the fs module name some functions of it this is one of the most important cont built-in modules in node in short it is used for managing files let's quickly see the code example first import the fs module by using the requir function like this then you can use the read file function of the fs module to read the contents of a file asynchronously here the first parameter is the name of the file from which we are reading the second parameter is the format of the file and the third parameter is this call back function in which we are passing two parameters the error and the data while so while reading from this fx. txt file if there is any error we got then we are just returning black okay and if there is no error then we are reading all the content of this fx. TST file in this variable data and printing content here in the console so I'm printing this is my logic and uh in as per the application requirement the logic can be different okay you can do anything with this data which we are getting from the file now similar to this we have another important function of the fs module called the right file function here the first parameter is the blank or empty file name this is blank where here we here we will write the content okay we this is write function then second parameter is the content source and the turn parameter is some same call back function to handle if there is any errors or any other scenario okay so that's it finally the definition of FS module is FS module in node provide a set of methods for interacting with the file system okay great now here are seven main functions of FS module read and write you already know append file used to append the new data with the existing data of a file then unlink function is used to delete the specified file then redirect function is used to read the content of directory sorry not redirect it's read directory di then mkd method is used to create a new directory or you can say folder then rmd method is used to remove a specified folder directory Great that is the answer to this question you do not have to remember the names of all these methods but the more you know about something the better you can explain it right explain the role of the path path module name some functions of it let's start with the definition the path module provides utilities for joining resolving parsing formatting normalizing and manipulating paths okay or you can say URLs for example here first we can import the path module and then here is the first me method of the path module which is join method very simple you can see this is used to join folders names and file names and it's creating a full path right very simple now second method of path module is very important this is parse method which is used for parsing okay now listen carefully passing means whatever string you pass here as a parameter our pass method will convert that string into an object and that object I am printing here see this object will will are always always have these properties root name directory or folder name base name file extension name and file name these are properties of the object so what happened basically now your path is string path is broken down into several parts and therefore now it is very easy to understand and reuse these broken parts right and that is the purpose of this Parts method uh so because now you can perform separate operations on these separate object properties as per your application requirement okay great so it is one of the most important function of the path module other than that I have mentioned here five more functions of path module and what they do that I already written here in the comments in very very simple words you can just pause the screen and read it once or you can later also download the book to revise it explain the role of the OS module name some functions of it the answer is OS module in nodejs projects provide a set of methods for interacting with the operating system very simple for that we will first import the OS module like this then we can use the type method of the OS module basically which will provide the type of the operating system on which your node application is running then you can get the user information of the operating system by using this user info method of the OS module okay so like this be the output then so also you can get the memory information of operating system in bites using the total memory method which provides the total memory and the free memory method which will provide the memory that is available in your operating system okay now you might ask why we need this operating system information in our node project okay so basically sometimes we deploy node projects on Windows sometimes on Linux okay so then basically servers can be different right different on different operating systems so then we have to perform some functionalities as per the operating systems to make our appliation crossplatform that is the main point okay because node is crossplatform one application can run on any operating system so for that sometimes we need operating system on information as well okay and the same thing I have written here also for later reference explain the role of events module how to handle events in node most of the things about events and this diagram I already explained in previous question right you remember here are the definition of events event emitter event Q event listener event Loop and event driven architecture all I already explained now let's see the event module how to implement in node for that first we will import the event module and then from that we will get a class event emitter okay now this class has many functions and properties and to use those functions first we will create the instance of this event object event emitter class like this so this we we call instance or object also now we can use on function of the my emitter object to register the event listener now you can ask okay so what is the meaning of registering the event listener or event handler now listen listen carefully registering an event listener means whenever whenever an event of this event name okay which is the first parameter whenever this event name will occur then the second parameter of this on function which is a complete function logic it will execute okay got my point so that is event listener so great so until now the event this event name is still has not occur okay in this code okay so let's uh create this event here see see we are raising or emitting the event event Name by using the emit emit function of the my emitter object only okay so what will happen now the above event listener will listen to it when this uh event will be emitted uh it will be created then above event listener will listen to it match the event name and run this function logic okay function Logic for it that's it clear great finally here you will find the output now remember in real application you will not emit events like this uh you will get some request from the front end application and then internally your node that request is a event only okay and then your node application will handle and then assign it to the get post putut okay which you will do in your code so and also remember here first we have to register the event listener before emitting the events if you Amit the event first and uh then you will register the event listener code that means event listener is not yet registered and hence you will get the error great that's the whole story of the event implementation in the code and here I have written a few points about uh again about event event ameter which I already explained and I hope you like this answer what are event arguments in the previous question I explained you this code right how can we import the events module get the event amiter class from them there we created the am Amer object then we registered the event listener by using the on method of the my emitter object now when we are emitting the event then we can also pass some additional information in the form of parameters like these argument one and argument two along with the event and that additional information is called event arguments these arguments will then be received by these parameter of the call back function above and then we can use them inside the function as per our application requirement Okay so in short if you want if you want to pass some data along with the event then you can use this event arguments and the same thing is written here also what is the difference between a function and an event sometimes we get confused by these kind of questions so let's remove the confusion 100% today what is a function first you already know a function is a reusable piece of code in your application that performs a specific task whenever that function is called right clear whereas events represent an action okay for example when your node API application gets some request from a frontend server like this that means some action happened and therefore therefore an event will be created in your node application okay now it may be that you can't see uh that it may be that that event code is there in some node library and so you can't actually see it and those events actually is handled by your node applica node back in background runtime and you can handle by using them in get postp put methods right so remember internally events can call single or multiple function to do the task so events are like when some event is created then function will be exe functions can also execute so that is the difference and that is the short answer to this this question same is I have written here whatever I have explained in a very short format what is the role of HTTP module in node this is one of the most important modules in node let's first quickly understand the request flow before understanding the role of the http AP module in a node application suppose you are a user and you open a node API based website in your browser so the request will first go to the UI server which will can be which can be in angular react JavaScript any front-end technology right now for static data like images Styles or for existing data uh existing data operation like if user will do sorting filter ing on the existing uh orders or in in e-commerce application for these kind of operation the UI server only will do it by itself and send the response back but if the user is requesting for additional data or something important then UI server will send the HTTP request with the API server Ur URL to your node application API now listen carefully to be able to receive these request from from the front end your API must be present or hosted at some URL address with the port number on the internet right so that hosting will be done by HTTP module so the HTTP module will create an HTTP server that will be listening to the HTTP request from the UI and then the same HTTP module will send back the HTTP response to the these UI and then UI will of course send the response to the user browser great I think now you know the role of the sttp module right uh in upcoming question we will quickly see the implementation also but the and and same thing I have written here the HTTP module can create an HTTP server that listen to server ports and gives a response back to the client that is the role of HTTP module in node what is the role of create server method of the HTTP module answer is very simple the create server method of the HTTP module in node is used to create an HTTP server now let me show you the code quickly and in five steps we will create the HTTP server by using HTTP module okay first step is import the http module second step is to create an HTTP server by using this create server method like this so basically here your server is now created and it is able to handle request and response which it is accepting as the parameter now here the second parameter of this create server function is itself a call back function right so which is basically the logic when you will receive the request which this logic will execute after you receive the request and before sending the response back this in between logic will execute got it okay so but right now it's not complete your server is still not able to listen to any request okay your server is created but it's not listening for that you can first choose a port number on which the server will listen the incoming request and finally by using this listen method of the above server object now your server is able to listen to the request at this port number clear great so finally to test this code test this code you run this command node create server.js which is the Javascript file name only and when you will open the browser and you will enter this URL Local Host with the port number and you can see the response of your node applications why you can see the response here in the browser because it is hosted by the HTTP module and here is this hello world is coming from the second parameter function of the create create server method great that's how we can create a server and enable it to listen the HTTP requests from the UI server that's the simple answer of this question all right if you reached here and completed all the questions and answers then great and my best wishes are with you first thing just post your resume and start giving the interviews okay I know it's hard but it's really very important and it's really very rewarding also be ready for rejections because this is the part of the interview Journey remember remember one selection and you will forgive forgive forget all your rejections and failures and the life will change from that day okay great and like these guys who shared their success stories with me whenever you get the offer share your journey with me in the comment section I will feel very proud and that is for my motivation basically again all the best and happy interviewing that's it
Info
Channel: Interview Happy
Views: 32,192
Rating: undefined out of 5
Keywords:
Id: Vdpx-9Oehdk
Channel Id: undefined
Length: 67min 22sec (4042 seconds)
Published: Sat Feb 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.