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
2D transformations in CSS allow us to perform som...
Not using lazy loading import Vue from 'vue...
Due to the needs of the work project, song playba...
Vue3 project encapsulation side navigation text s...
sed is a character stream editor under Unix, that...
Table of contents 1. Concept 1.1 Definition 1.2 D...
Author: Guan Changlong is a DBA in the Delivery S...
Download MySQL for Mac: https://downloads.mysql.c...
illustrate In front-end development, you often en...
There are too much knowledge to learn recently, a...
Result: The main part is to implement the code lo...
Today, let’s get straight to the point and talk a...
Download from official website: https://www.mysql...
Function Origin I was recently working on an H5 t...
Usually in project development, we have to deal wi...