What is index.js used for in node.js projects?

后端 未结 4 521
死守一世寂寞
死守一世寂寞 2020-12-02 16:50

Other than a nice way to require all files in a directory (node.js require all files in a folder?), what is index.js used for mainly?

4条回答
  •  醉酒成梦
    2020-12-02 17:21

    Late to the party but the answer is simply to allow a developer to specify the public api of the folder!

    When you have a bunch of JavaScript files in a folder, only a small subset of the functions and values exported from these files should exportable outside of the folder. These carefully selected functions are the public apis of the folder and should be explicitly exported (or re-exported) from the index.js file. Thus, it serves an architectural purpose.

提交回复
热议问题