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
HTML is a hybrid language used for publishing on ...
How to solve the Mysql transaction operation fail...
Before installing Tomcat, install the JDK environ...
1. Install basic components First, execute the yu...
The specific code is as follows: package epoint.m...
Recently, I encountered the need to embed a player...
This article shares the specific code for impleme...
MySQL handles duplicate data Some MySQL tables ma...
This article shares the summary of the JS mineswe...
Tomcat is widely known as a web container. It has...
Recently I changed Apache to nginx. When I moved ...
Preface Sometimes when hover pseudo-class adds a ...
Table of contents About FastDFS 1. Search for ima...
React originated as an internal project at Facebo...
System: Ubuntu 16.04LTS 1\Download mysql-5.7.18-l...