NodeJS

Introduction

  • It provides javascript runtime environment which is similar with browsers

  • It is used V8 Engine to execute javascript code

Lifecycle & Event loop

const http = require("http");

const server = http.createServer((req,res)=>{
    ...
});

server.listen(3000);

  • The server which is also a event listener is registered, the event loop process keep on running and handle the callback function

  • As the javascript is single-thread, in order to make it non-blocking and handle multiple request efficiently, there is a event loop to handle the callback of event.

  • Nodejs use event-driven programming . for each connection to the server, it is treated as an event and event handler as a callback function.

  • E.g: Handling File I/O: the file system operation will be handled by worker pool which is responsible for heavy-lifting and detached from the javascript thread . When finished, the callback of function will be queued and then executed

  • There is sequence for queue of event loop

  • nextTick queue is the highest, which is process.nextTick()

  • jobTask queue follows to nextTick queue, which is the callback of promise

  • task queue follows to the job queue

  • Here is the example to show the sequence of the event loop tasks

  • Here is the result

Reference

Last updated

Was this helpful?