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

Select does not support double click dbclick event

XML/HTML CodeCopy content to clipboard < div c...

React error boundary component processing

This is the content of React 16. It is not the la...

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

Some notes on modifying the innodb_data_file_path parameter of MySQL

Preface innodb_data_file_path is used to specify ...

Theory: The two years of user experience

<br />It has been no more than two years sin...

In-depth analysis of MySQL explain usage and results

Preface In daily work, we sometimes run slow quer...

A brief discussion on the application of Html web page table structured markup

Before talking about the structural markup of web...

How to use js to determine whether a file is utf-8 encoded

Conventional solution Use FileReader to read the ...

Some useful meta setting methods (must read)

<meta name="viewport" content="...

Play and save WeChat public account recording files (convert amr files to mp3)

Table of contents Audio transcoding tools princip...

mysql8 Common Table Expression CTE usage example analysis

This article uses an example to describe how to u...

Analysis of the configuration process of installing mariadb based on docker

1. Installation Search the mariadb version to be ...