NodeJS - 需求和模块

NodeJS - Require and Modules

Does require and module.exports in NodeJS 可用于获取驻留在目录中的所有 JavaScript 文件中的所有函数,而不是单个 JavaScript 文件?
如果是,怎么做?
谁能用例子解释一下

通过使用要求 您需要该文件中的模块,您可以使用该原型(单个文件)而不是完整目录的所有功能。 例如

    function admin(admin_id)
{
    //console.log(parent_id);
    this.admin_id = admin_id;
}
//default constructor
function admin()
{
    admin_id = null;
    self =this;
}
//destructor
~function admin(){
    this.admin_id = null;
    console.log('admin obj destroyed!');
    }
//exporting this class to access anywhere through data encapstulation
module.exports = admin;
//class methods

admin.prototype =  {

        help:function(params){

console.log('hi');
}

    }, 

你可以要求这个模块并且可以使用功能帮助 通过这种方法,您可以在单个文件中要求所有文件(模块)

Wiki:“Node.js 是用于开发服务器端 Web 应用程序的开源跨平台运行时环境。

虽然Node.js不是JavaScript框架,但是它的很多基础模块都是用JavaScript编写的,开发者可以在JavaScript中编写新的模块。

运行时环境使用 Google 的 V8 JavaScript 引擎解释 JavaScript。"

Nodejs 示例:

你有Afile.js

var Afile = function()
{

};

Afile.prototype.functionA = function()
{
    return 'this is Afile';
}

module.exports = Afile;

和Bfile.js

var Bfile = function()
{

};

Bfile.prototype.functionB = function()
{
    return 'this is Bfile';
}

module.exports = Bfile;

Test.js 文件需要 Afile.js 和 Bfile.js

var Afile                         = require(__dirname + '/Afile.js');
var Bfile                         = require(__dirname + '/Bfile.js');

var Test = function()
{

};

Test.prototype.start = function()
{
    var Afile = new Afile();
    var Bfile = new Bfile();

    Afile.functionA();
    Bfile.functionB();
}

var test = Test;
test.start();

如果为 require 指定了目录路径,它将在该目录中查找 index.js 文件。因此,将您的模块特定的 js 文件放在一个目录中,创建一个 index.js 文件并最终在您的工作 js 文件中需要该目录。希望下面的示例有所帮助....

示例:

文件:modules/moduleA.js

function A (msg) {
    this.message = msg;
}
module.exports = A;

文件:modules/moduleB.js

function B (num) {
    this.number = num;
}
module.exports = B;

文件:modules/index.js

module.exports.A = require("./moduleA.js");
module.exports.B = require("./moduleB.js");

文件:test.js

var modules = require("./modules");
var myMsg = new modules.A("hello");
var myNum = new modules.B("000");
console.log(myMsg.message);
console.log(myNum.number);