What are the built-in modules of nodejs?
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.
What are the built-in modules of nodejs?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main differences between Node.js and Tomcat are: Runtime: Node.js is based on JavaScript runtime, while Tomcat is a Java Servlet container. I/O model: Node.js uses an asynchronous non-blocking model, while Tomcat is synchronous blocking. Concurrency handling: Node.js handles concurrency through an event loop, while Tomcat uses a thread pool. Application scenarios: Node.js is suitable for real-time, data-intensive and high-concurrency applications, and Tomcat is suitable for traditional Java web applications.

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
