如何在路径中要求模块具有模式?

时间:2022-04-13 20:26:15

How can I include all files in nodeJS like

如何在nodeJS中包含所有文件

require('./packages/city/model/cities')
require('./packages/state/model/states')
require('./packages/country/model/countries')

like as

require('./packages/*/model/*')

same like grunt is loading files.

像grunt一样正在加载文件。

3 个解决方案

#1


4  

You can't (or at least you shouldn't)

你不能(或至少你不应该)

In order to do this, you would have to overload node's native require function, which is highly inadvisable.

为了做到这一点,你必须重载节点的本机需求函数,这是非常不可取的。

The CommonJS pattern might seem tedious to you, but it's a very good one and you shouldn't try to break it just because you saw shortcuts in other languages/frameworks.

CommonJS模式对你来说可能看起来很乏味,但它是一个非常好的模式,你不应该因为你看到其他语言/框架中的快捷方式而试图打破它。

By introducing some form of magic in your module, you suddenly change everything that programmers can (and should be able to) safely assume about the CommonJS pattern itself.

通过在模块中引入某种形式的魔法,你会突然改变程序员可以(并且应该能够)安全地假设CommonJS模式本身的所有内容。

#2


2  

Due to one-to-one correspondence in node module loading system, it wont be possible natively, but would not be surprised if there is a package for this method.

由于节点模块加载系统中的一对一对应,本机不可能,但如果有这种方法的包,也不会感到惊讶。

Best you can do is create a index.js that loads modules present in directory and exports them as its own.

您可以做的最好的事情是创建一个index.js,它加载目录中的模块并将它们作为自己的模块导出。

module.exports = function() {
   return {
        city : require('./city/model/'),
        state : require('./packages/state/model/'),
        country : require('./packages/country/model/')
   }
} 

you would have to load models in similary fashion in all three dirrectories as well.

你也必须在所有三个dirrectories中以类似的方式加载模型。

I know that this solution is not what you are looking for but in my experirence, this method allows to better manage custom packages as you can add/remove features easily.

我知道这个解决方案不是你想要的,但在我的实验中,这种方法可以更好地管理自定义包,因为你可以轻松添加/删除功能。

#3


1  

Node.js's require allows you to

Node.js的要求允许你

  1. load only one module at a time

    一次只加载一个模块

  2. load modules only in synchronous fashion.

    仅以同步方式加载模块。

That is how the module system works in Node.js. But if you want to have minimatch kind of matching functionality, you can roll one on your own, like this

这就是模块系统在Node.js中的工作方式。但是如果你想拥有minimatch类型的匹配功能,你可以自己滚动一个,就像这样

var path = require("path"),
    glob = require("glob");

function requirer(pattern) {
    var modules = {},
        files = glob.sync(pattern);

    files.forEach(function(currentFile) {
        var fileName = path.basename(currentFile);
        fileName = fileName.substring(0, fileName.lastIndexOf(".js"));
        modules[fileName] = require(currentFile);
    });

    return modules;
}

This depends on glob module, which allows you to use minimatch patterns to search files and then we require the found files, store them in an object and return the object. And this can be used like this

这取决于glob模块,它允许您使用迷你匹配模式搜索文件,然后我们需要找到的文件,将它们存储在一个对象中并返回该对象。这可以像这样使用

var modules = requirer('./packages/*/model/*.js');
console.log(modules.cities);

P.S: I am working on making this a public module already.

P.S:我正在努力使其成为一个公共模块。

#1


4  

You can't (or at least you shouldn't)

你不能(或至少你不应该)

In order to do this, you would have to overload node's native require function, which is highly inadvisable.

为了做到这一点,你必须重载节点的本机需求函数,这是非常不可取的。

The CommonJS pattern might seem tedious to you, but it's a very good one and you shouldn't try to break it just because you saw shortcuts in other languages/frameworks.

CommonJS模式对你来说可能看起来很乏味,但它是一个非常好的模式,你不应该因为你看到其他语言/框架中的快捷方式而试图打破它。

By introducing some form of magic in your module, you suddenly change everything that programmers can (and should be able to) safely assume about the CommonJS pattern itself.

通过在模块中引入某种形式的魔法,你会突然改变程序员可以(并且应该能够)安全地假设CommonJS模式本身的所有内容。

#2


2  

Due to one-to-one correspondence in node module loading system, it wont be possible natively, but would not be surprised if there is a package for this method.

由于节点模块加载系统中的一对一对应,本机不可能,但如果有这种方法的包,也不会感到惊讶。

Best you can do is create a index.js that loads modules present in directory and exports them as its own.

您可以做的最好的事情是创建一个index.js,它加载目录中的模块并将它们作为自己的模块导出。

module.exports = function() {
   return {
        city : require('./city/model/'),
        state : require('./packages/state/model/'),
        country : require('./packages/country/model/')
   }
} 

you would have to load models in similary fashion in all three dirrectories as well.

你也必须在所有三个dirrectories中以类似的方式加载模型。

I know that this solution is not what you are looking for but in my experirence, this method allows to better manage custom packages as you can add/remove features easily.

我知道这个解决方案不是你想要的,但在我的实验中,这种方法可以更好地管理自定义包,因为你可以轻松添加/删除功能。

#3


1  

Node.js's require allows you to

Node.js的要求允许你

  1. load only one module at a time

    一次只加载一个模块

  2. load modules only in synchronous fashion.

    仅以同步方式加载模块。

That is how the module system works in Node.js. But if you want to have minimatch kind of matching functionality, you can roll one on your own, like this

这就是模块系统在Node.js中的工作方式。但是如果你想拥有minimatch类型的匹配功能,你可以自己滚动一个,就像这样

var path = require("path"),
    glob = require("glob");

function requirer(pattern) {
    var modules = {},
        files = glob.sync(pattern);

    files.forEach(function(currentFile) {
        var fileName = path.basename(currentFile);
        fileName = fileName.substring(0, fileName.lastIndexOf(".js"));
        modules[fileName] = require(currentFile);
    });

    return modules;
}

This depends on glob module, which allows you to use minimatch patterns to search files and then we require the found files, store them in an object and return the object. And this can be used like this

这取决于glob模块,它允许您使用迷你匹配模式搜索文件,然后我们需要找到的文件,将它们存储在一个对象中并返回该对象。这可以像这样使用

var modules = requirer('./packages/*/model/*.js');
console.log(modules.cities);

P.S: I am working on making this a public module already.

P.S:我正在努力使其成为一个公共模块。