What naming conventions exist for the primary Node

2019-05-25 05:34发布

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.

4条回答
神经病院院长
2楼-- · 2019-05-25 05:45

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

查看更多
Deceive 欺骗
3楼-- · 2019-05-25 05:47

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.

查看更多
放我归山
4楼-- · 2019-05-25 05:50

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.

查看更多
做个烂人
5楼-- · 2019-05-25 05:54

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.

查看更多
登录 后发表回答