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:
-
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.
-
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.
-
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.
-
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.
-
Storage Layer:
- Contains definitions for functions and server-related methods:
- doOnIncoming
- doOnError
-
server object (with listen and on methods).
Request Flow Overview:
- A HTTP request enters the system.
- The socket is opened to process the request With Port (80/443)
- The request passes through Libuv, which interacts with the system's networking and file system capabilities.
- 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!