Detailed explanation of nodejs built-in modules

Detailed explanation of nodejs built-in modules

Overview

Nodejs built-in modules refer to the beauty provided in addition to the default syntax. You don’t need to download them and can directly import them. Just write the name when you import them.

Nodejs built-in modules:

1. Path module

Used to process file paths.

path.normalize (path parsing, get the canonical path);

path.join(path merge);

path.resolve (get the absolute path);

path.relative (get relative path).

......

2. Until module

To make up for the lack of js functions, add new API.

util.format (format output string);

util.isArray (check if it is an array);

util.RegExp(is it regular);

util.isDate(Is it a date type);

util.inherits(child,parent) implements inheritance;

3. fs module

File system API

fs.readFile(filename,[options],callback); Read file.

fs.writeFile(filename,data,[options],callback); write file.

fs.appendFile(filename,data,[options],callback); writes the file in append mode.

fs.open(filename,flags,[mode],callback); opens the file.

filename: file name, required.

data: written data or buffer stream.

flags: operation flag, opening mode, rw.

[options]: Specify permissions: read, write, and execute. Whether it can be continued.

callback: callback function after reading the file. function (err, data);

fs.mkdir(path,[mode],callback); creates a directory.

fs.readdir(path,callback);Read the directory.

fs.exists(path,callback); Check whether the file and directory exist.

fs.utimes(path,atime,mtime,callback); Modify the access time and modification time of a file.

fs.rename(oldfilename,newfilename,callback); Rename the file name or directory.

fs.rmdir(path,callback); deletes an empty directory.

path: The full path and directory name of the directory to be created.

[mode]: Directory permissions, default is 0777 (readable, writable, and executable).

atime: New access time.

ctime: New modification time.

oldfilename, newfilename The old name and the new name.

callback: callback function after the directory is created.

4. Events module

The events module provides only 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 that 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 sequence, and the event parameters are passed as callback function parameters.

5. http module

http.createServer(function(){});Create a server.

http.get('path', callback); sends a get request.

http.request(options,callback);Send the request.

options: options is an associative array-like object that represents the request parameters. callback is a callback function that needs to pass a parameter.

Common options parameters include host, port (default is 80), method (default is GET), and path (the path of the request relative to the root, the default is "/").

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();

post:

var http = require("http");
var querystring = require("querystring");
 
var postData = querystring.stringify({
    "content":"I'm really just testing it",
    "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=IzZDJiMGU0OTMyNTE0ZGFhZDAzZDNhZTAyZDg2ZmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjkyOTk0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         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("Comment completed!");
    });
    console.log(res.statusCode);
});
 
req.on("error",function(err){
    console.log(err.message);
})
req.write(postData);
req.end();

6. Jade module

Jade is a high-performance, simple and easy-to-understand template engine. You can use jade to write HTML files.

Jade is similar to a language used to quickly write HTML, and the suffix of the written file is .jade.

7. Express Framework

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

To use it, open the path where you want to create the web project in cmd. Then enter

Express appname

You can create a web project named appname.

The above is a detailed explanation of the nodejs built-in modules. For more information about nodejs built-in modules, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of NodeJS modularity
  • Node.js API detailed explanation of util module usage example analysis
  • Analysis of the usage of common tool modules util based on nodejs
  • Node.js common tools util module
  • Util module in node.js tutorial example detailed explanation

<<:  Example analysis of mysql stored procedure usage

>>:  Steps to set up Windows Server 2016 AD server (picture and text)

Recommend

Issues with upgrading Python and installing Mongodb drivers under Centos

Check the Python version python -V If it is below...

Standard summary for analyzing the performance of a SQL statement

This article will introduce how to use explain to...

Detailed explanation of how to use Vue to load weather components

This article shares with you how to use Vue to lo...

jQuery achieves breathing carousel effect

This article shares the specific code of jQuery t...

How to modify the time in centos virtual machine

The one above shows the system time, and the one ...

Linux kernel device driver memory management notes

/********************** * Linux memory management...

How to implement dual-machine master and backup with Nginx+Keepalived

Preface First, let me introduce Keepalived, which...

How to deploy kafka in docker

Table of contents 1. Build Docker 2. Enter the co...

Use non-root users to execute script operations in docker containers

After the application is containerized, when the ...

About the layout method of content overflow in table

What is content overflow? In fact, when there is ...

An example of using Dapr to simplify microservices from scratch

Table of contents Preface 1. Install Docker 2. In...

How to implement element floating and clear floating with CSS

Basic Introduction to Floating In the standard do...

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...