nodejs uses js module

Intro

Recently, nodejs needs to be used as a crawler. Google has a project of puppeter, which can be used as a crawler. There are many introductions about puppeter on the Internet, which will not be described in detail here. node Xiaobai, I was a bit confused at the beginning, and module export was not possible.

The official document says that *. mjs is supported, but the file extension needs to be changed. It's a bit strange and useless. It's mainly based on the module of js.

Two methods of module export

Because I am familiar with C ා, from my understanding of C ා, the module export in nodejs is divided into two forms:

  1. A module to be called by instantiation
  2. A static class that can be called without instantiation, providing some static methods
  • Export a class to instantiate

    module.exports = exports = function (){ };

    module.exports = exports = function() {
    
      this.syncCompanyList = async function(developerName){
          await syncCompanyInfo(developerName);
      };
    
      async function syncCompanyInfo(developerName){
          // ...
      }
    }
  • Export a static class

    exports.funcName = function (){};

    var getDistrictCode = function (districtName) {
        if (districtName) {
            for (let i= 0; i< DistrictInfo.length; i++) {
                let district = DistrictInfo[i];
                if (district["name"] == districtName || district["aliasName"] == districtName) {
                    return district["code"];
                }
            }
        }
        return "";
    };
    
    var getNormalDistrictName = function (districtName) {
        if (districtName) {
            if (districtName.indexOf('area') > 0) {
                return districtName;
            }
            for (let i= 0; i< DistrictInfo.length; i++) {
                let district = DistrictInfo[i];
                if (district["name"] == districtName || district["aliasName"] == districtName) {
                    return district["name"];
                }
            }
        }
        return "";
    }
    
    // Set methods and properties of export
    exports.getDistrictCode = getDistrictCode;
    exports.getNormalDistrictName = getNormalDistrictName;

Referencing exported module methods

Use require in node to reference module

  • Reference npm package

    const log4js = require("log4js");
  • Refer to the module written by yourself

    const districtUtil = require("./utils/districtUtil");

Using exported modules

To use a module, you need to refer to a module first, and the reference module can refer to the previous step.

  • Instance class

    const company = require("./company");
    // ...
    // Instantiate a company object
    var comp = new company();
    // Call syncCompanyList in company 
    comp.syncCompanyList ();
  • Static class

    const districtUtil = require("./utils/districtUtil");
    // ...
    // Call getDistrictCode in districtUtil
    let districtNme = districtUtil.getDistrictCode('districtName');

End

I hope you can get something

Tags: Javascript Google npm

Posted on Tue, 03 Dec 2019 14:37:09 -0500 by knsito