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

前端 未结 4 502
-上瘾入骨i
-上瘾入骨i 2021-02-01 14:22

In node.js, it seems I run into the same 3 filenames to describe the main entry point to an app:

  • When using the express-generator package, an
4条回答
  •  遇见更好的自我
    2021-02-01 14:39

    In fact all are just names and you must be consistent in your own work as pointed out by @Oka in a previous answer.

    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. It seems the best choice if you are developing an NPM package.

    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.

提交回复
热议问题