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

后端 未结 4 514
死守一世寂寞
死守一世寂寞 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:07

    Typically in other languages the web server looks for certain files to load first when visiting a directory like / in priority, traditionally this is either: index or default. In php it would be index.php or just plain HTML it would be index.html

    In Node.js, Node itself is the web server so you don't need to name anything index.js but it's easier for people to understand which file to run first.

    index.js typically handles your app startup, routing and other functions of your application and does require other modules to add functionality. If you're running a website or web app it would also handle become a basic HTTP web server replacing the role of something more traditional like Apache.

提交回复
热议问题