In order for Node.js files to be able to call each other, Node.js provides a simple module system.

Modules are a fundamental part of node.js applications, and files and modules correspond one to one. In other words, a Node.js file is a module, which could be JavaScript code, JSON, or a compiled C/C++ extension.

1. Create a custom module

Using exports

exports.reqest = function(){
    console.log('request request')}exports.get = function(){
    console.log('get request')}exports.post = function(){
    console.log('post request')}Copy the code

Exports using module.exports

var app = {
    name: 'the weather'.version: '1.0.0'.build: function(){
        console.log('build app')}}module.exports = app
Copy the code

Module import use

const module1 =  require('./custom_module.js')
const module2 =  require('./custom_module2.js')

/ / use
console.log(module2.name)
module1.get()
Copy the code

2. NPM installs tripartite modules

As described above, using custom modules, NodeJS provides a number of official and third-party developer written modules to help simplify development. Nodejs modules can be found on NPM using https://www.npmjs.com/

Install the three-party module. Here is a brief introduction to the installationaxios, this is a very common network request module, has a very powerful function.

npm install axios --save
Copy the code

In this way, the module AXIos is added to the project and dependency concerns are written to the package.json file. You can pull down the project somewhere else and use NPM I or NPM install.

Sometimes, you may need to install a specific version. You can install it using @+ version number

npm install axios@1.0. 0 --save
Copy the code

3. Fs module

Introducing fs module

const fs = require('fs')
Copy the code
  • fs.statCheck whether it is a folder or a file
// Check whether it is a file or a directory
fs.stat('./impor_module.js'.function(error, data){
    if(error){
        console.log("Error:" + error);
        return;
    }

    if(data.isFile){
        console.log("Yes.");
    }else{
        console.log("Yes folder"); }})Copy the code
  • fs.mkdirCreate a directory
// Check whether the directory exists
fs.exists('./scc'.function(exists){
    if(! exists){// If it does not exist, create a directory
        fs.mkdir('./scc'.function(error){
            if(error){
                console.log(error)
                return
            }
            console.log('Directory created successfully! ')})}else{
    	// The directory already exists. Go to subsequent operations
        console.log('Directory already exists')}})Copy the code

To create folders, we can use a tripartite module if we want to create multiple layers at once, greatly reducing our operations.

npm install -g mkdirp
Copy the code

use

var mkdirp = require('mkdirp');
    
mkdirp('/tmp/foo/bar/baz'.function (err) {
    if (err) console.error(err)
    else console.log('pow! ')});Copy the code

For FS module, I will briefly introduce two, the subsequent use of other time, you can find their own documentation.

Reference:

1, Node. Js module system: www.runoob.com/nodejs/node…

Nodejs exports and module.exports: blog.csdn.net/qq_31967569…

3, NPM mkdirp use of commonly used modules: www.cnblogs.com/jiaoshou/p/…