Nodejs built-in modules: 1. path module, used to process file paths, the introduction syntax is "require('path')"; 2. until module, used to make up for the lack of js functions and add new APIs; 3. fs module, file operations System API; 4. events module, which provides an "events.EventEmitter" object; 5. jade module, which can write html files through jade.
The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.
The built-in modules of nodejs refer to the beauty provided in addition to the syntax provided by default. There is no need to download it. It can be introduced directly. Just write the name.
nodejs built-in modules:
1. path module
Used to handle file paths.
path.normalize (path analysis, get the canonical path);
path.join(path merge);
path.resolve(get absolute path);
path.relative(get relative path).
...
2. until module
To make up for the lack of js functions, new APIs are added.
util.format (formatted output string);
util.isArray(check if it is an array);
util.RegExp (whether it is regular);
util.isDate (whether it is date type);
util.inherits(child,parent) implements inheritance;
3. fs module
File operating system API
fs.readFile(filename,[options],callback); Read the file.
fs.writeFile(filename,data,[options],callback);Write file.
fs.appendFile(filename,data,[options],callback); Write the file in append mode.
fs.open(filename,flags,[mode],callback); Open the file.
filename: file name, required.
data: written data or buffer stream.
flags: operation flag, opening method, rw.
[options]: Specify permissions, read, write, execute. Whether it can be continued.
callback: callback function after reading the file. function(err,data);
fs.mkdir(path,[mode],callback);Create 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 the file.
fs.rename(oldfilename,newfilename,callback);Rename the file name or directory.
fs.rmdir(path,callback); Delete empty directories.
path: The full path and directory name of the created directory.
[mode]: Directory permissions, default 0777 (readable, writable and executable).
atime: new access time.
ctime: new modification time.
oldfilename, newfilename old name and new name.
callback: The callback function after the directory is created.
4. 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.
5. http module
http.createServer(function(){});Create a server.
http.get('path',callback);Send get request.
http.request(options,callback);Send request.
options: options is an object similar to an associative array, representing the parameters of the request. As a callback function, callback needs to pass a parameter.
Commonly used parameters of options include host, port (default is 80), method (default is GET), path (the requested path 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 this", "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=IzZDJiMGU0OTMyNTE0ZGFhZDAzZDNhZTAyZDg2ZmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjkyOTk0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA VhMTYwNzRmMjczNjdmZWUyNDg1ZTZkMGM1BwhXVwcIV1c%3DMD; phpSESSID=thh4bfrl1t7qre9tr56m32tbv0; 3719,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 complete! "); }); 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, 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 framework
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.
To use it, open the path of the web project you want to create in cmd. Then enter
Express appname
You can create a web project named appname.
Recommended learning: "nodejs video tutorial"
The above is the detailed content of the built-in modules of nodejs. For more information, please pay attention to other related articles on this site!