Home > Web Front-end > JS Tutorial > Create Server in node under the hood

Create Server in node under the hood

Mary-Kate Olsen
Release: 2024-12-15 01:01:10
Original
812 people have browsed it

Create Server in node under the hood

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);
    
    Copy after login
  • 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/

The above is the detailed content of Create Server in node under the hood. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template