什么是索引。js通常用于节点中。js项目

时间:2021-10-25 23:58:01

Other than a nice way to require all files in a directory (node.js require all files in a folder?), what is index.js used for mainly; does it have a 'best practices' utility?

除了一种要求目录(节点)中的所有文件的好方法之外。js需要文件夹中的所有文件),什么是索引?js用于主要;它有“最佳实践”的效用吗?

2 个解决方案

#1


27  

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()时,它将检查包。json对于一个端点。如果没有定义,它会检查索引。js,最后指数。节点(c++扩展格式)。因此,索引。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:

此外,您还会询问如何要求目录中的所有文件。通常,需要有索引的目录。向这些文件公开一些封装接口的js;这样做的方式将会不同于以往的模块。但是,假设您希望在包含文件夹时包含一个文件夹的内容(注意,这不是最佳实践,而且出现的次数比您想象的要少)。然后,你可以使用一个索引。同步加载目录中的所有文件(异步设置导出通常会导致严重的错误)并将它们附加到模块。出口一样:

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

我几乎没见过有人想要使用这种模式——大多数时候你想要索引。像这样

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

#2


10  

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服务器在访问/ in priority这样的目录时首先查找要加载的某些文件,通常是:index或default。在php中,它是索引。php或普通的HTML,它是index.html

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

在节点。Node本身就是web服务器,所以不需要命名任何索引。但是人们更容易理解先运行哪个文件。

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.

索引。js通常处理应用程序的启动、路由和应用程序的其他功能,并且需要其他模块添加功能。如果您正在运行一个网站或web应用程序,它也将处理成为一个基本的HTTP web服务器,取代更传统的Apache的角色。

#1


27  

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()时,它将检查包。json对于一个端点。如果没有定义,它会检查索引。js,最后指数。节点(c++扩展格式)。因此,索引。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:

此外,您还会询问如何要求目录中的所有文件。通常,需要有索引的目录。向这些文件公开一些封装接口的js;这样做的方式将会不同于以往的模块。但是,假设您希望在包含文件夹时包含一个文件夹的内容(注意,这不是最佳实践,而且出现的次数比您想象的要少)。然后,你可以使用一个索引。同步加载目录中的所有文件(异步设置导出通常会导致严重的错误)并将它们附加到模块。出口一样:

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

我几乎没见过有人想要使用这种模式——大多数时候你想要索引。像这样

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

#2


10  

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服务器在访问/ in priority这样的目录时首先查找要加载的某些文件,通常是:index或default。在php中,它是索引。php或普通的HTML,它是index.html

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

在节点。Node本身就是web服务器,所以不需要命名任何索引。但是人们更容易理解先运行哪个文件。

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.

索引。js通常处理应用程序的启动、路由和应用程序的其他功能,并且需要其他模块添加功能。如果您正在运行一个网站或web应用程序,它也将处理成为一个基本的HTTP web服务器,取代更传统的Apache的角色。