DEV Community

Ashutosh Sarangi
Ashutosh Sarangi

Posted on

Create Server in node under the hood

Image description

The image appears to be a conceptual diagram explaining how a Node.js server processes incoming HTTP requests.
Here's a description of the components and their relationships as depicted in the diagram:

Main Components:

  1. Node.js Server Code:

    • The code snippet demonstrates setting up an HTTP server in Node.js:
     const doOnIncoming = (req, res) => {};
     const doOnError = (error, data) => {};
    
     const server = http.createServer();
    
     server.listen(80);
     server.on('request', doOnIncoming);
     server.on('error', doOnError);
    
  • Functions:
    • doOnIncoming: Handles incoming requests.
    • doOnError: Handles server errors.
  • server.listen(80): Starts the server to listen on port 80.
  • Event handlers:
    • 'request': Triggers the doOnIncoming function.
    • 'error': Triggers the doOnError function.
  1. HTTP Request Flow:

    • A request (e.g., http://twitter/3) is sent to the server.
    • It is received as a Buffer through a socket connection.
  2. Libuv and Computer Features:

    • Libuv acts as the bridge between Node.js and system-level operations:
      • Handles networking and file system tasks.
    • Manages asynchronous I/O operations.
  3. Node.js/C++ Features:

    • Auto-added arguments:
      • req (Request Object): Contains details like body and header.
      • res (Response Object): Provides methods such as send(), status(), and json().
    • Auto-executed functions:
      • doOnIncoming: Processes the request and sends a response.
      • doOnError: Handles and logs errors.
  4. Storage Layer:

    • Contains definitions for functions and server-related methods:
      • doOnIncoming
      • doOnError
      • server object (with listen and on methods).

Request Flow Overview:

  1. A HTTP request enters the system.
  2. The socket is opened to process the request With Port (80/443)
  3. The request passes through Libuv, which interacts with the system's networking and file system capabilities.
  4. Node.js handles the request using predefined functions (doOnIncoming and doOnError) and sends a response back.

This diagram illustrates the interplay between JavaScript, Node.js, and the system's underlying C++ features, providing a clear picture of how Node.js processes requests at a low level.

Reference:-
https://frontendmasters.com/courses/servers-node-js/

Top comments (0)