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, the API of the file operating system; 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
is 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
Make up for the lack of js functions and add new APIs.
util.format(formatted output string);
util.isArray(check whether it is an array);
util.RegExp(whether it is regular);
util.isDate (is it 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 identification, opening method, r w.
[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: callback function after creating the directory.
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 a 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.
Options commonly used parameters 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":"我真的只是测试一下", "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();
6. jade module
jade is a high-performance, concise and easy-to-understand template engine. It can Write html files 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.
Usage method, open the path of the web project you want to create in cmd. Then enter
Express appname
to create a file named appname's web project.
Recommended learning: "nodejs video tutorial"
The above is the detailed content of What are the built-in modules of nodejs?. For more information, please follow other related articles on the PHP Chinese website!