Home Web Front-end Front-end Q&A What are the official modules of nodejs?

What are the official modules of nodejs?

Nov 11, 2021 pm 05:05 PM
nodejs

Nodejs official modules include: 1. path module, used to process file paths; 2. until module; 3. fs module, API of file operating system; 4. events module; 5. http module; 6. jade module; 7. Express module.

What are the official modules of nodejs?

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

Nodejs built-in modules refer to the beauty provided in addition to the default syntax. No need to download, they can be imported directly. Just write the name.

nodejs built-in module:

1. Path module:

is used to process file paths .

path.normalize(路径解析,得到规范路径);
path.join(路径合并);
path.resolve(获取绝对路径);
path.relative(获取相对路径)。
......
Copy after login

2. Until module:

Make up for the lack of js functions and add new APIs.

util.format(格式化输出字符串);
util.isArray(检查是否为数组);
util.RegExp(是不是正则);
util.isDate(是不是日期型);
util.inherits(child,parent)实现继承;
Copy after login

3. fs module:

API of file operating system

fs.readFile(filename,[options],callback); 读取文件。
fs.writeFile(filename,data,[options],callback);写文件。
fs.appendFile(filename,data,[options],callback);以追加的方式写文件。
fs.open(filename,flags,[mode],callback); 打开文件。
filename:文件名,必须。
data:写入的数据或者buffer流。
flags:操作标识,打开方式,r w。
[options]:指定权限,读、写、执行。是否可续写。
callback:读取文件后的回调函数。function(err,data);
fs.mkdir(path,[mode],callback);创建目录。
fs.readdir(path,callback);读取目录。
fs.exists(path,callback);查看文件与目录是否存在。
fs.utimes(path,atime,mtime,callback);修改文件的访问时间和修改时间。
fs.rename(oldfilename,newfilename,callback);重命名文件名或者目录。
fs.rmdir(path,callback);删除空目录。
path:被创建目录的完整路径以及目录名。
[mode]:目录权限,默认0777(可读可写可执行)。
atime:新的访问时间。
ctime:新的修改时间。
oldfilename、newfilename  旧名字和新名字。
callback:创建完目录后的回调函数。
Copy after login

4. events module

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.

5. http module

http.createServer(function(){});创建服务器。
http.get('路径',callback);发送get请求。
http.request(options,callback);发送请求。
options:options是一个类似关联数组的对象,表示请求的参数,callback作为回调函数,需要传递一个参数。
options常用的参数有host、port(默认为80)、method(默认为GET)、path(请求的相对于根的路径,默认是“/”。
Copy after login

get:

var http=require("http");
 var options={
    hostname:"cn.bing.com",
    port:80
}
 
var req=http.request(options,function(res){
    res.setEncoding("utf-8");
    res.on("data",function(chunk){
        console.log(chunk.toString())
    });
    console.log(res.statusCode);
});
req.on("error",function(err){
    console.log(err.message);
});
req.end();
Copy after login

post:

var http=require("http");
var querystring=require("querystring");
 
var postData=querystring.stringify({
    "content":"我真的只是测试一下",
    "mid":8837
});
 
var options={
    hostname:"www.imooc.com",
    port:80,
    path:"/course/document",
    method:"POST",
    headers:{
        "Accept":"application/json, text/JavaScript, */*; q=0.01",
        "Accept-Encoding":"gzip, deflate",
        "Accept-Language":"zh-CN,zh;q=0.8",
        "Connection":"keep-alive",
        "Content-Length":postData.length,
        "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie":"imooc_uuid=6cc9e8d5-424a-4861-9f7d-9cbcfbe4c6ae; imooc_isnew_ct=1460873157; loginstate=1;
         apsid=IzZDJiMGU0OTMyNTE0ZGFhZDAzZDNhZTAyZDg2ZmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         AAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjkyOTk0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         AAAAAAAAAAAAAAAAAAAAAAAAAAAAAGNmNmFhMmVhMTYwNzRmMjczNjdmZWUyNDg1ZTZkMGM1BwhXVwcIV1c%3DMD;
          phpSESSID=thh4bfrl1t7qre9tr56m32tbv0; 
          Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1467635471,1467653719,1467654690,1467654957;
           Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1467655022; imooc_isnew=2;
            cvde=577a9e57ce250-34",
        "Host":"www.imooc.com",
        "Origin":"http://www.imooc.com",
        "Referer":"http://www.imooc.com/video/8837",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) 
        AppleWebKit/537.36 (Khtml, like Gecko) Chrome/53.0.2763.0 Safari/537.36",
        "X-Requested-With":"XMLHttpRequest",
    }
}
 
var req=http.request(options,function(res){
    res.on("data",function(chunk){
        console.log(chunk);
    });
    res.on("end",function(){
        console.log("评论完毕!");
    });
    console.log(res.statusCode);
});
 
req.on("error",function(err){
    console.log(err.message);
})
req.write(postData);
req.end();
Copy after login

6. jade module

jade is a high-performance, concise and easy-to-understand template engine. HTML files can be written through jade.

jade is similar to a language for quickly writing HTML, and the suffix of the written file is .jade.

7. Express module

Express is a nodejs web open source framework, used to quickly build web projects. It mainly integrates the creation of web http servers, static text management, server URL address request processing, get and post request processing and distribution, session processing and other functions.

Usage method, open the path of the web project you want to create in cmd. Then enter

Express appname
Copy after login

to create a web project named appname.

[Recommended learning: "nodejs tutorial"]

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

See all articles