Nodejs learning log 3-- module

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Introduction of nodejs module with require:
var express = require('express')

The exports object is used for methods and variables to export the current module, a file is a module, the mount on the exports object as the attribute definition derived, for example:

exports.test1 = function(){
  return 'ok';

In another file, we through the require () method is introduced into the model, methods and attributes can call it:

var test = require('test'); = function(){
    return test.test1();

Introducing modules need to experience three steps in node:

  1. Path analysis.
  2. File location
  3. The compiler implementation

Path analysis:
require()Method accepts an identifier as a parameter, module identifier into:

File location:

Module compiler:

The node module as the core module and the module to write your own (module), The core module is compiled in the node source code in compiled into binary executable file, When starting the process part of the core module is directly loaded into memory, Skipping 2 (file location) and 3 (compilation and execution), And in the 1 (path analysis) when the judgment of priority, The loading rate is the fastest, File module in dynamic loading operation, Requires 123 steps, The loading speed slow than the core module. Node will be introducing a cache module, the object cache is compiled and executed after. require()Method first checks the cache cache loading, check the core module of the prior to the file module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Adrian at January 07, 2014 - 1:05 AM