简体   繁体   English

node.js 中 app.js、index.js 和 server.js 的约定?

[英]Conventions for app.js, index.js, and server.js in node.js?

In node.js, it seems I run into the same 3 filenames to describe the main entry point to an app:在 node.js 中,我似乎遇到了相同的 3 个文件名来描述应用程序的主要入口点:

  • When using the express-generator package, an app.js file is created as the main entry point for the resulting app.使用express-generator包时,会创建一个app.js文件作为生成的应用程序的主要入口点。
  • When creating a new package.json file via npm init , one is prompted for the main entry point file.通过npm init创建新的package.json文件时,系统会提示输入主入口点文件。 The default is given as index.js .默认值为index.js
  • In some programs I have seen, server.js serves as the main entry point as well.在我见过的一些程序中, server.js作为主要入口点。

Other times, still, it seems as though there are subtle differences in their usage.其他时候,它们的用法似乎仍然存在细微的差异。 For example, this node app directory structure uses index.js and server.js in different contexts:例如,这个 node app 目录结构在不同的上下文中使用index.jsserver.js

app
  |- modules
  |    |- moduleA
  |    |    |- controllers
  |    |    |    |- controllerA.js
  |    |    |    +- controllerB.js
  |    |    |- services
  |    |    |    +- someService.js
  |    |    +- index.js <--------------
  |    +- index.js <-------------------
  |- middleware.js
  +- index.js <------------------------
config
  +- index.js <------------------------
web
  |- css
  |- js
server.js <----------------------------

What are the differences, if any, between these three names?这三个名称之间有什么区别(如果有)?

Even though you can call the files anything you want, there's an advantage to calling the entry point index.js or server.js尽管您可以随意调用文件,但调用入口点 index.js 或 server.js 还是有优势的

Why index.js: When you issue npm init it will set the main entry point of the module to index.js.为什么使用 index.js:当您发出npm init它会将模块的主入口点设置为 index.js。 Some people don't change it, so they end up naming their main entry point index.js.有些人不改变它,所以他们最终将他们的主要入口点命名为 index.js。 It means there's one less thing to do.这意味着少了一件事情要做。

Why server.js: If your node package is not going to be consumed by another package, but rather is a stand-alone app, then if you call your main entry point server.js, then you can issue npm start and start your app.为什么是 server.js:如果你的 node 包不会被另一个包消耗,而是一个独立的应用程序,那么如果你调用你的主入口点 server.js,那么你可以发出npm start并启动你的应用程序. npm start will run your server.js file by default. npm start默认会运行你的 server.js 文件。 To change this behavior, supply a start script in package.json.要更改此行为,请在 package.json 中提供start脚本。 If a start script exists, npm start will run that script instead.如果存在start脚本, npm start将运行该脚本。

app.js is just a convention -- the only advantage to it is that some IDEs, such as Visual Studio Code will default to app.js as the entry point of a program you debug. app.js 只是一个约定——它的唯一优势是某些 IDE,例如 Visual Studio Code 将默认将 app.js 作为您调试程序的入口点。 That way when using the most common framework, Express, which creates an app.js file, "it just works"这样,当使用最常见的框架 Express 时,它会创建一个 app.js 文件,“它只是有效”

It's pretty simple!这很简单!

If your application is to be used in other applications: index.js如果您的应用程序要在其他应用程序中使用: index.js

If your application is NOT to be used in other applications: server.js or app.js如果您的应用程序不用于其他应用程序: server.jsapp.js

As stated earlier the reasons being, when invoking npm start , if not defined in package.json , looks automatically for server.js .如前所述,原因是,在调用npm start ,如果package.json未定义,则会自动查找server.js And when including another module into your application, it looks for index.js .当将另一个模块包含到您的应用程序中时,它会查找index.js

Extra: I also tend to only use index.js as a file name when this is automatically found somehow.额外:当以某种方式自动找到时,我也倾向于只使用index.js作为文件名。 This makes me aware if the file is invoked directly or indirectly.这让我知道该文件是直接调用还是间接调用。

When using npm init it makes the default index.js .使用npm init 时,它会生成默认的index.js

Where I work, we didn't really settle on a format, so we have some apps with index.js, some with server.js.在我工作的地方,我们并没有真正确定一种格式,所以我们有一些带有 index.js 的应用程序,一些带有 server.js 的应用程序。 Also, in some we have a config.js file at the root level, others are in a config folder (so require(config/config.js). We even have one where server.js is in a server folder.此外,有些我们在根级别有一个 config.js 文件,其他人在 config 文件夹中(所以 require(config/config.js)。我们甚至有一个 server.js 在服务器文件夹中。

The trouble comes when we want to automate our deployment process.当我们想要自动化我们的部署过程时,麻烦就来了。 It becomes like technical debt when we have to make a bunch of minor modifications for each service.当我们必须对每项服务进行大量小的修改时,这就像是技术债务。

That said, pick a format that makes sense to you and stick with it.也就是说,选择一种对您有意义的格式并坚持下去。

In fact all are just names and you must be consistent in your own work as pointed out by @Oka in a previous answer.事实上,一切都只是名字,正如@Oka 在之前的回答中指出的那样,您必须在自己的工作中保持一致。

The only valid point here is that modular nature of node may play an important role in your decision, as pointed out in Folders as Modules section of NodeJS documentation there are 3 ways in which a folder may be passed to require() as an argument and the second and a common one is to automatically load the index.js file from the folder , this is how a lot of NPM packages are built and as it is simple and standard according to automatically loading NodeJS feature.这里唯一有效的一点是 node 的模块化性质可能在您的决定中发挥重要作用,正如 NodeJS 文档的Folders as Modules部分所指出的,有 3 种方法可以将文件夹作为参数传递给 require() 和第二个也是最常见的一个是自动从文件夹中加载 index.js 文件,这就是很多 NPM 包的构建方式,因为它根据自动加载 NodeJS 功能简单而标准。 It seems the best choice if you are developing an NPM package.如果您正在开发 NPM 包,这似乎是最佳选择。

In the end, as others pointed out, you can choose any of the three, or even another one, but stick to your decision.最后,正如其他人所指出的,您可以选择三个中的任何一个,甚至另一个,但要坚持自己的决定。 My decision was to always use index.js based on the mentioned fact above.基于上述事实,我的决定是始终使用 index.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM