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

左心房为你撑大大i 提交于 2020-01-02 13:26:26

问题


This question has been completely edited in hopes that it will be reopened.

The naming of the main Node.js file is something left to the user and and does not seem to be defined by any well established convention. In hopes of finding a good name, I am curious if there are naming conventions in other parts of the Node.js ecosystem that might suggest a name to use.

Some names I have seen are: app.js, index.js, main.js, server.js, etc.

Please provide only well documented standards in answers.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/19850028/what-naming-conventions-exist-for-the-primary-node-js-file

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