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

前端 未结 4 501
-上瘾入骨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:47

    It's pretty simple!

    If your application is to be used in other applications: index.js

    If your application is NOT to be used in other applications: server.js or app.js

    As stated earlier the reasons being, when invoking npm start, if not defined in package.json, looks automatically for server.js. And when including another module into your application, it looks for index.js.

    Extra: I also tend to only use index.js as a file name when this is automatically found somehow. This makes me aware if the file is invoked directly or indirectly.

    When using npm init it makes the default index.js.

提交回复
热议问题