In node.js, it seems I run into the same 3 filenames to describe the main entry point to an app:
express-generator
package, an app.js
file is created as the main entry point for the resulting app.package.json
file via npm init
, one is prompted for the main entry point file. The default is given as index.js
.server.js
serves as the main entry point as well.Other times, still, it seems as though there are subtle differences in their usage. For example, this node app directory structure uses index.js
and server.js
in different contexts:
app
|- modules
| |- moduleA
| | |- controllers
| | | |- controllerA.js
| | | +- controllerB.js
| | |- services
| | | +- someService.js
| | +- index.js <--------------
| +- index.js <-------------------
|- middleware.js
+- index.js <------------------------
config
+- index.js <------------------------
web
|- css
|- js
server.js <----------------------------
What are the differences, if any, between these three names?
js is where you would usually mount/render your main react component onto your “root” element(which you mark in your html). “App” is what people tend to call the file which contains the main logic of your file, or in React case, the main component, the one that represents your entire application/web-site.
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.
Let's Create our Node. To run this file, you should open up your terminal again and navigate to the directory in which you placed index. js. Once you successfully navigated yourself to the right spot, run your file using the node index. js command.
Even though you can call the files anything you want, there's an advantage to calling the entry point index.js or server.js
Why index.js:
When you issue npm init
it will set the main entry point of the module to index.js. Some people don't change it, so they end up naming their main entry point index.js. It means there's one less thing to do.
Why server.js:
If your node package is not going to be consumed by another package, but rather is a stand-alone app, then if you call your main entry point server.js, then you can issue npm start
and start your app. npm start
will run your server.js file by default. To change this behavior, supply a start
script in package.json. If a start
script exists, npm start
will run that script instead.
app.js is just a convention -- the only advantage to it is that some IDEs, such as Visual Studio Code will default to app.js as the entry point of a program you debug. That way when using the most common framework, Express, which creates an app.js file, "it just works"
It's pretty simple!
If your application is to be used in other applications: index.js
If your application is NOT to be used in other applications: server.js or app.js
As stated earlier the reasons being, when invoking npm start
, if not defined in package.json
, looks automatically for server.js
. And when including another module into your application, it looks for index.js
.
Extra:
I also tend to only use index.js
as a file name when this is automatically found somehow. This makes me aware if the file is invoked directly or indirectly.
When using npm init it makes the default index.js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With