What naming conventions exist for the primary Node.js file?

只谈情不闲聊 提交于 2019-12-06 11:08:12

NPM seems to suggest a standard whereby one can define the primary file in the package.json file like so:

"scripts": {"start": "node server.js"}

If no such property is set, NPM looks for a server.js file in the root of the package. If server.js exists, it will be run with Node.

This default seems to be a strong suggestion that the name server.js should be the standard.

index.js has a special usage in Node.js. From the Module docs.

...

If there is no package.json file present in the directory, then node will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:

./some-library/index.js

./some-library/index.node

I prefer to use app.js or even main.js

The two predominant filenames are 'app.js' & 'server.js'. Its better to go with 'server.js'. This is for nodejs applications. In the case of libraries, most libraries use 'index.js' and specify it in their 'main' param in the package.json file.

From what I have seen, app.js is the most universally accepted.

I personally prefer server.js, but this is probably biased in that I run a massive Single Page Application so all my files are .... javascript ..... and I have an app.js controller for my front-end. So it helps me distinguish the two.

do you know how well-established conventions start? you make a decision that is logical, and forthrightly forward it with resolute certainty - then others will follow. there clearly is no well established convention, so pick one and tell it to others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!