In NodeJS, there are two types of modules, system modules and custom modules.

System module

Nodejs: NodeJS: require() : nodeJS: nodeJS: nodeJS: nodeJS: nodeJS: nodeJS: nodeJS: nodeJS: nodeJS: nodeJS: nodeJS

  • Path module: formalized path
const path=require('path');
var str='c:\\wamp\\www\\a.html'; var obj=path.parse(str); //base file name part //ext extension //dir path //name file name part console.log(obj);Copy the code
  • Fs module: read and write operations to files


This is the contents of 1.txtCopy the code


const fs = require('fs')
var str = fs.readFile('./1.txt'.'utf-8'// This is the asynchronous operation console.log(STR) // this is the contents of 1.txtCopy the code

The second way to write it

fs.readFile('./1.txt', (err, data) => {
  if (err) throw err;
Copy the code
  • Querystring: Parsed string

For example, the query string ‘foo=bar&abc=xyz&abc=123’ is parsed to:

  foo: 'bar',
  abc: ['xyz'.'123']}Copy the code
  • Url: The module is used to process and parse urls

The usage is as follows

const url = require('url');
Copy the code

For the URL example, it is recommended that you actually operate on the document, personally recommended.

Custom modules

Custom modules are modules that we can write ourselves. Here I will briefly introduce the basic use of custom modules.

First, in NodeJS, a JS file can be called a module.

To use our module you must have two operations, export and import.


Var a = 1 module. Exports = a / / export a or module. The export = {a} / / here can export multiple properties or methodsCopy the code


var moduleA = require('./a.js'Console. log(modulea.a) // Output 1Copy the code

Publish our modules through NPM

Here I simply say the release method, specific we can go to Baidu, that is more detailed than me

1. Register an NPM account

2. Log in to the NPM using the CMD command line interface

3. Publish directly to your NPM repository via NPM command (very crude…)

This is the basics of nodeJS modularity. If there is something wrong with it, please feel free to make fun of it.