index.js 在 node.js 项目中的用途是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21063587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:38:09  来源:igfitidea点击:

What is index.js used for in node.js projects?

node.jslibraries

提问by alh

Other than a nice way to require all files in a directory (node.js require all files in a folder?), what is index.jsused for mainly?

除了需要目录中所有文件的好方法(node.js 需要文件夹中的所有文件?),index.js主要用于什么?

回答by MattNewton

When you pass a folder to Node's require(), it will check for a package.json for an endpoint. If that isn't defined, it checks for index.js, and finally index.node (a c++ extension format). So the index.js is most likely the entry point for requiring a module.

当您将文件夹传递给 Node 的 require() 时,它会检查端点的 package.json。如果没有定义,它会检查 index.js,最后检查 index.node(c++ 扩展格式)。所以 index.js 很可能是需要一个模块的入口点。

See the official Docs here: http://nodejs.org/api/modules.html#modules_folders_as_modules.

在此处查看官方文档:http: //nodejs.org/api/modules.html#modules_folders_as_modules

Also, you ask how to require all the files in a directory. Usually, you require a directory with an index.js that exposes some encapsulated interface to those files; the way to do this will be different for ever module. But suppose you wanted to include a folder's contents when you include the folder (note, this is not a best practice and comes up less often than you would think). Then, you could use an index.js that loads all the files in the directory synchronously (setting exports asynchronously is usually asking for terrible bugs) and attaches them to module.exports like so:

此外,您询问如何要求目录中的所有文件。通常,您需要一个带有 index.js 的目录,该目录向这些文件公开一些封装的接口;执行此操作的方法将因模块而异。但是假设您想在包含文件夹时包含文件夹的内容(请注意,这不是最佳实践,而且出现的频率比您想象的要低)。然后,您可以使用 index.js 同步加载目录中的所有文件(异步设置导出通常会导致可怕的错误)并将它们附加到 module.exports ,如下所示:

var path = require('path'),
    dir = require('fs').readdirSync(__dirname + path.sep);

dir.forEach(function(filename){

    if(path.extname(filename) === '.js' && filename !== 'index.js'){
        var exportAsName = path.basename(filename);
        module.exports[exportAsName] = require( path.join( __dirname, filename) );
    }

});

I hardly ever see people wanting to use that pattern though - most of the time you want your index.js to go something like

我几乎没有看到人们想要使用这种模式 - 大多数时候你希望你的 index.js 像这样

var part1 = require('./something-in-the-directory'),
    part2 = require('./something-else');
....
module.exports = myCoolInterfaceThatUsesPart1AndPart2UnderTheHood;

回答by Steven Leggett

Typically in other languages the web server looks for certain files to load first when visiting a directory like / in priority, traditionally this is either: index or default. In php it would be index.php or just plain HTML it would be index.html

通常在其他语言中,Web 服务器在优先访问像 / 之类的目录时会首先查找要加载的某些文件,传统上这是:索引或默认值。在 php 中它会是 index.php 或者只是普通的 HTML 它会是 index.html

In Node.js, Node itself is the web server so you don't needto name anything index.js but it's easier for people to understandwhich file to run first.

在 Node.js 中,Node 本身就是 Web 服务器,因此您无需为 index.js 命名任何内容,但人们更容易理解首先运行哪个文件。

index.js typically handles your app startup, routing and other functions of your application and does require other modules to add functionality. If you're running a website or web app it would also handle become a basic HTTP web server replacing the role of something more traditional like Apache.

index.js 通常处理您的应用程序启动、路由和应用程序的其他功能,并且确实需要其他模块来添加功能。如果您正在运行一个网站或 Web 应用程序,它也会处理成为一个基本的 HTTP Web 服务器,取代像 Apache 这样更传统的东西的角色。

回答by onmyway133

Here is a good article explaining how Node.js looks for required module https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8, with folder and index.jsfile

这是一篇很好的文章,解释了 Node.js 如何查找所需模块https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8,包含文件夹和index.js文件

Modules don't have to be files. We can also create a find-me folder under node_modules and place an index.js file in there. The same require('find-me') line will use that folder's index.js file:

模块不必是文件。我们还可以在 node_modules 下创建一个 find-me 文件夹,并在其中放置一个 index.js 文件。相同的 require('find-me') 行将使用该文件夹的 index.js 文件:

~/learn-node $ mkdir -p node_modules/find-me
~/learn-node $ echo "console.log('Found again.');" > node_modules/find-me/index.js
~/learn-node $ node
> require('find-me');
Found again.
{}
>

回答by alaboudi

Late to the party but the answer is simply to allow a developer to specify the public api of the folder!

迟到了,但答案只是允许开发人员指定文件夹的公共 api!

When you have a bunch of JavaScript files in a folder, only a small subset of the functions and values exported from these files should exportable outside of the folder. These carefully selected functions are the public apis of the folder and should be explicitly exported (or re-exported) from the index.js file. Thus, it serves an architectural purpose.

当文件夹中有一堆 JavaScript 文件时,只有一小部分从这些文件导出的函数和值应该可以导出到文件夹之外。这些精心挑选的函数是文件夹的公共 api,应该从 index.js 文件中明确导出(或重新导出)。因此,它服务于架构目的。