Node.JS Express - 将文件夹导入为模块

时间:2021-07-11 11:48:19

I am new in express.

我是快递新人。

What I am trying to do is import a folder as a module so that in future I can use other files in the folder can be used as modules.

我要做的是导入一个文件夹作为模块,以便将来我可以使用该文件夹中的其他文件可以用作模块。

So, what I have done is change this-

所以,我所做的就是改变这一点 -

var routes = require('./routes/index');
var users = require('./routes/users');

app.use('/', routes);
app.use('/users', users);

to this-

var routes = require('./routes');

app.use('/', routes.index);
app.use('/users', routes.users);

But I am getting error like this-

但我得到的错误是这样的 -

Node.JS Express  - 将文件夹导入为模块

Can anyone please help, what can I do to solve this issue so that I can use all files inside route folder as modules?

任何人都可以请求帮助,我该怎么做才能解决这个问题,以便我可以使用路径文件夹中的所有文件作为模块?

Update-

Regarding to @AnubhavSrivastava , my requirement is OK because I have seen this in MVA tutorial. Link is here and time is 1:32:58.

关于@AnubhavSrivastava,我的要求是可以的,因为我在MVA教程中看到了这一点。链接在这里,时间是1:32:58。

Code is like this-

代码是这样的 -

Node.JS Express  - 将文件夹导入为模块

Thanks in advance for helping.

在此先感谢您的帮助。

1 个解决方案

#1


1  

when you do require over a folder, it returns index.js module by default, you wont get route.user , unless you have a variable that is exported in index.js.

当你需要一个文件夹时,它默认返回index.js模块,你不会得到route.user,除非你有一个在index.js中导出的​​变量。

Extract from node js official documentation

从节点js官方文档中提取

Folders as Modules

It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library. There are three ways in which a folder may be passed to require() as an argument.

将程序和库组织到自包含目录中很方便,然后为该库提供单个入口点。有三种方法可以将文件夹传递给require()作为参数。

The first is to create a package.json file in the root of the folder, which specifies a main module. An example package.json file might look like this:

第一种是在文件夹的根目录中创建一个package.json文件,该文件指定一个主模块。示例package.json文件可能如下所示:

{ "name" : "some-library", "main" : "./lib/some-library.js" }

{“name”:“some-library”,“main”:“。/ lib / some-library.js”}

If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.

如果这是在./some-library的文件夹中,则require('。/ some-library')会尝试加载./some-library/lib/some-library.js。

This is the extent of Node.js's awareness of package.json files.

这是Node.js对package.json文件的认知程度。

Note: If the file specified by the "main" entry of package.json is missing and can not be resolved, Node.js will report the entire module as missing with the default error:

注意:如果package.json的“main”条目指定的文件丢失且无法解析,则Node.js将报告整个模块缺失,并显示默认错误:

Error: Cannot find module 'some-library' If there is no package.json file present in the directory, then Node.js will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:

错误:无法找到模块'some-library'如果目录中没有package.json文件,则Node.js将尝试从该目录中加载index.js或index.node文件。例如,如果上例中没有package.json文件,则require('。/ some-library')会尝试加载:

./some-library/index.js ./some-library/index.node

#1


1  

when you do require over a folder, it returns index.js module by default, you wont get route.user , unless you have a variable that is exported in index.js.

当你需要一个文件夹时,它默认返回index.js模块,你不会得到route.user,除非你有一个在index.js中导出的​​变量。

Extract from node js official documentation

从节点js官方文档中提取

Folders as Modules

It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library. There are three ways in which a folder may be passed to require() as an argument.

将程序和库组织到自包含目录中很方便,然后为该库提供单个入口点。有三种方法可以将文件夹传递给require()作为参数。

The first is to create a package.json file in the root of the folder, which specifies a main module. An example package.json file might look like this:

第一种是在文件夹的根目录中创建一个package.json文件,该文件指定一个主模块。示例package.json文件可能如下所示:

{ "name" : "some-library", "main" : "./lib/some-library.js" }

{“name”:“some-library”,“main”:“。/ lib / some-library.js”}

If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.

如果这是在./some-library的文件夹中,则require('。/ some-library')会尝试加载./some-library/lib/some-library.js。

This is the extent of Node.js's awareness of package.json files.

这是Node.js对package.json文件的认知程度。

Note: If the file specified by the "main" entry of package.json is missing and can not be resolved, Node.js will report the entire module as missing with the default error:

注意:如果package.json的“main”条目指定的文件丢失且无法解析,则Node.js将报告整个模块缺失,并显示默认错误:

Error: Cannot find module 'some-library' If there is no package.json file present in the directory, then Node.js will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:

错误:无法找到模块'some-library'如果目录中没有package.json文件,则Node.js将尝试从该目录中加载index.js或index.node文件。例如,如果上例中没有package.json文件,则require('。/ some-library')会尝试加载:

./some-library/index.js ./some-library/index.node