A callback function is a function which can be passed as an argument into another function. This callback function can then be invoked inside the outer function. Callback functions were introduced in JavaScript. Since Node.js built on V8 engine, it also support callback functions. In the above example, doComplete is a callback function. This program can also be written … [Read more...] about Callback Function in Node.js
Node.js
Basic web application in Node.js to return text
Here we write a basic web application in Node.js that serves static text to the client's HTTP requests. Added an event listener on the server variable that listens to the request events. The event listener takes a callback function with two arguments, request and response.If there is a request, node.js will raise a request event adding the respective request listener to … [Read more...] about Basic web application in Node.js to return text
Why does a while loop blocks the event loop?
How Node.js process this program? flag variable declaration will be placed in call stack and it will be initialized to true.setTimeout() function call will be placed in call stack. Node.js start the timer and schedules a timer event for 1 second. The callback function will be placed in MessageQueue after 1 second.Node.js continues further and starts executing the while … [Read more...] about Why does a while loop blocks the event loop?
Read File Synchronously and Asynchronously (Blocking and Non-blocking ways)
In this post we will learn to read a text file synchronously with a blocking code and asynchronously by writing non-blocking code. Node.js provides methods to read a file both synchronously and asynchronously in it's 'fs' module. Synchronously reading a file using blocking methods of Node.js: Asynchronously reading a file using non-blocking methods of Node.js: … [Read more...] about Read File Synchronously and Asynchronously (Blocking and Non-blocking ways)
Simple Web Server Program in Node.js
Here we write a simple web server program in Node.js that serves static text to the client's HTTP requests. The createServer method returns a http.Server objectWe are passing requestListener callback function as a parameter to createServer which automatically added to the 'request' eventhttp.Server emits 'request' event when it receives a requestThe request event passes … [Read more...] about Simple Web Server Program in Node.js