Summary of several modules commonly used by node. js

Source: Internet
Author: User

/*
* One util
* It's a tool module inside node, and almost all of the modules in node will be used in this module
function
* 1 Implementation Inheritance This is the main feature
* 2 Implement the full output of the object
* 3 Implementation of the data type of judgment
*
*/

Introducing Tool Classes
var util = require ("util");

function Parent () {
Inherit content within the parent function for internal inheritance
THIS.name = "Papa";
This.age = "50";
This.sayhello = function () {
Console.log ("Hello");
}
}
External inheritance for inherited properties of parent
Parent.prototype.getName = function () {
Console.log (this.name);
}

function Child () {
Do not use inherits to implement inheritance
Parent.call (this);
}
Implement Child inheritance Parent
The inherits of the tool class util can inherit only the methods and properties of the prototype chain (prototype) and cannot inherit the methods and properties of the class-owned (directly written in the class).
Util.inherits (child,parent);
Console.log (Child)

/*
*path Module
*
* Function: Format canonical path
*
*/
Introducing Modules
var path = require ("path");
var str = ".. /D\/D/FG/GG//.L ";
Formatting a path that is not canonical
Console.log (Path.normalize (str));

Stitching Path
var res = Path.join ("s/b/c/d", "... /f ");
Console.log (RES);


/*
*FS Module
* 1 operation of the file
* 2 Operating Directory
*/
/*
* File section (both have synchronous and asynchronous distinction here only to discuss async)
*/
VarFS =Require"FS");
1 reading files
Fs.readfile ("1.txt", "Utf-8", function (Error,data) {
if (Error) {
Console.log ("Failed to read file");
return Console.error (Error);
}else{
Console.log (data);
// }
//})

2 Writing Files
Parameters: 1 Path 2 file contents 3 Optional parameters (information of file contents) 4 callback function
For parameter 1, if the path does not exist, a new file will be created, and if present, the content will be written in the original file

Fs.writefile ("Lxl.txt", "Hello World", {
Flag: "A",//a does not overwrite last run result W overwrite last run result
Encoding: "Utf-8"
},function (Error) {
//
//});
2 second way to write files
Fs.appendfile ("Hhh.txt", "real fake?", function (error,data) {//append stitching defaults to a
//
//});


1 FS itself does not provide a copy method, its own implementation of a
function Copy (SRC,DESC) {
Fs.readfile (SRC, "utf-8", function (Error,data) {
if (Error) {
//
}else{
Read success, write new file
Fs.writefile (desc,data,{flag: "W"},function (Error) {
//
// })
// }
// })
//}
//
Copy ("Lxl.txt", "ssss.txt");




Directory Operations Section
1. Reading the Directory
Fs.Readdir"Dir1",function (err,files) {
if (err) {
Console.Log"Read failed")
}else{
Console.log (files);
}
});
2. Create a Directory
Fs.mkdir"Dir2",0777,Function (Error) {
if (Error) {
Console.Log"Create folder Failed");
}else{
Console.Log"Create folder Success");
}
});
3. Determine if a file or folder exists
Fs.Exists"1.jpg",function (exist) {
Console. log (exist);
})
4. Modify the file name
FS. rename ("1.jpg","2.jpg",function (err) {
if (err) {
Console. log ("failed to modify");
}else{

}
})
/*
*
* The previous file processing method (mainly refers to reading data and writing data methods, for the small files).
* But for big data processing, readile and WriteFile have some problems to deal with.
* The way to use stream stream for big data processing
*/

Introducing the FS module
VarFS =Require"FS");
Create a read data stream
VarRead =Fs.Createreadstream ("Video.mp4");
Implementing a copy of a video
VarWR =Fs.Createwritestream ("Copy Video.mp4");
Stream can be used to deal with big data because it separates the data into a small piece of small pieces to handle, not the whole file as a whole.
A small piece (64KB)
var times = 0;
Read.on ("Data", function (chunk) {
//times + +;
//wr.write (chunk);
//
//})
//Read.on ("End", function () {
//Console.log (times);
//})

//pipe is a pipeline that is used to connect the input stream and output stream, read the stream of data, write directly through the pipeline to the write stream inside
read. pipe (WR);
 

Summary of several modules commonly used by node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.