What are the core modules of nodejs?

青灯夜游
Release: 2021-11-11 17:55:07
Original
5837 people have browsed it

The core modules of nodejs: 1. HTTP module, used to process network requests from clients; 2. URL module, used to process URLs requested by clients; 3. Query Strings module; 4. File System module ; 5. Path module; 6. Global module and so on.

What are the core modules of nodejs?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.

HTTP module of Node core module

Function:

Process the client’s network request

Code Steps:

1. Import the HTTP core module
2. Listen to the client’s request
3. Process the client’s request
4. Start the server

Node Core Module URL Module

Function:

Process the URL requested by the client

Code steps:

1. Import the URL core module
2. Import the HTTP core module
3. Monitor the client’s request
Process the URL requested by the client
4. Process the client's request
5. Start the server

Query Strings module of NodeJS core module

##Function:

Processing parameters passed by the client through `get/post` request

Key points to use:

1. Need to import the core module 'querystring'

2. When making a `get` request, querystring is generally used in conjunction with the url core module
3. `get/post` requests ultimately need to call the querystring.parse method to convert the key-value pair string passed by the request. into a js object for easy operation

Note:

The request method of get/post is different. When the client passes it over, the parameters are placed in

The places are different, so the server-side processing methods are also different

File System module of NodeJS core module

Premise:

All future static resources (html, css, js, pictures, etc.) are placed on the server side. If the browser needs these html, css, js, pictures and other resources, it needs to be read into node.exe first. content, and then return it to the browser

Function:

To operate files on the server side, you may need to save the pictures uploaded by the browser to the server, or It may be necessary to read the server's resources and return them to the browser

Code steps:

1. Import the fs core module

2. Use the corresponding method to write files, read Fetch files and other operations

Path module of NodeJS core module

Function

The path of the operation file is File operation service

Global module of NodeJS core module

Function:

Globally shared, no need to import the module You can use

Commonly used attributes:

__dirname: The path to the folder where the file is located

__filename: The path to the file
require(): Import requirements Module
module: used when customizing the module
exports: used when customizing the module

NodeJS core module events module## The #events module only provides one object: events.EventEmitter.

[The core of EventEmitter is the encapsulation of event triggering and event listener functions. 】

Each event of EventEmitter consists of an event name and several parameters. The event name is a string, which usually expresses certain semantics. For each event, EventEmitter supports several event listeners. When an event is triggered, the event listeners registered to this event are called in turn, and the event parameters are passed as callback function parameters.

[Recommended learning: "

nodejs tutorial

"]

The above is the detailed content of What are the core modules of nodejs?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template