1. Commonjs
2. Two solutions for module export Solution 1 let str={}; module.exports=str; Solution 2 let str={}; exports.A=str; 3. Writing custom modules common.js // Built-in modules and custom modules in node // The module exports two solutions let str={}; module.exports=str; exports.A=str; // To import a module, use require("") to load the module let todo = require("./todo"); // The suffix can be omitted console.log(todo); todo.js module.exports={ name:"Zhang San", sleep:function(){ console.log("sleep"); } } or module.exports={ name:"Zhang San", sleep:function(){ console.log("sleep"); } }
Case 1 common.js // To import a module, use require("") to load the module let todo = require("./todo"); // The suffix can be omitted console.log(todo); // When requiring() in node, you can directly write the name when loading a module, but it must be loaded under the dependency, and a configuration file must be generated // Enter the dependency file in the terminal and install the configuration file let fetch=require("Fetch"); console.log(fetch); fetch.get("http://www.zjm.com"); Fetch.js module.exports={ get(url){ console.log(url); } } Case 2 common.js let Axios = require("Axios"); let url = "https://autumnfish.cn/search"; let data = { keywords: 'Xi'an' }; const http = require("http"); let app = http.createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); Axios.get(url, { params: data }).then((result) => { res.write(result); res.end(); }); }); app.listen(8080) Axios.js const http = require("http"); const https = require("https"); //Conversion method let change = (args) => { let str = "?"; for (let key in args) { str += key + "=" + args[key]; str += "&"; } return str.slice(0, str.length - 1); } module.exports = { get(href, { params }) { return new Promise((resolve, reject) => { href += change(params); https.get(href, (res) => { let { statusCode } = res; let error; if (statusCode != 200) { error = new Error('Request Failed.\n' + `Status Code: ${statusCode}`); } if (error) { console.error(error.message); // Consume response data to free up memory res.resume(); return; } //Set the response encoding res.setEncoding("utf8"); let alldata = ""; //Monitor datares.on("data", (info) => { alldata += info; }) res.on("end", () => { let data =alldata; resolve(data); }) }); }); }, post() { } } This is the end of this article about the implementation of built-in modules and custom modules in Node.js. For more relevant Node.js built-in modules and custom modules, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of MySQL user rights verification and management methods
>>: How to configure Linux CentOS to run scripts regularly
Table of contents 1. Download 2. Deployment 3. Ng...
HTML5 and jQuery implement the preview of local i...
Based on Vue and native javascript encapsulation,...
Table of contents Application Scenario Simply put...
The Docker images we usually build are usually la...
1. Check the synchronization status of A and B da...
Docker provides a way to automatically deploy sof...
Table of contents introduce start Install ① Direc...
ask: I have styled the hyperlink using CSS, but i...
What is text wrapping around images? This is the ...
Table of contents 1. Overview 2. Routing Navigati...
Table of contents Creating HTML Pages Implement t...
Table of contents 1. Communication method between...
Table of contents Constructor new Operator Implem...
Starting from IE 8, IE added a compatibility mode,...