Why use bin/www and not index.js?
Asked Answered
L

2

8

The express-generator tool creates a file called bin/www and uses it as the application's main entry-point. I believe I've seen a couple of other modules do this as well, but the vast majority simply use index.js.

What is the rationale behind this? Of course I understand why you would split the server and the code for setting up the program into a separate modules, but why bin/www and not index.js? Why nest the main-entry point to a program two levels deeper than the stuff it calls? And remove the file-extension, making it even less descriptive?

Is there a clever, non-obvious reason behind this? Should I use this for my node-modules as well?

Thank you!

[edit]:

All good answers, thank you folks! I've accepted the one pointing out that this is standard behaviour for packages that include executables. Here's some more reading I've come across on this:

Lentissimo answered 1/4, 2019 at 8:47 Comment(0)
K
6

You're used to running npm run, but not sysadmin. He will look for executables (attribute x) in thebin directory.

The entry point index.js is for node module. All packages that provide commands to run on the console contain the bin directory.

The extension is removed because it is not a script, but as a program. And these do not have extensions.

Kreegar answered 1/4, 2019 at 10:29 Comment(0)
G
4

express-generator create a basic structure for an express application. By convention, the entry point of the app is index.js or app.js. In fact, express-generator create an app.js at the root of the application with the initial setup of express.

Also by convention, the bin/ directory is used for binary files, and by extension for the scripts you can directly launch (note the shebang at the first line of www file). This is common on Linux that binary file has no extension and it could explain the choice to keep this habit for this file.

www, by convention again, is used for naming web application (like /var/www/html in Apache server)

Anyway, as the documentation says,

The app structure created by the generator is just one of many ways to structure Express apps. Feel free to use this structure or modify it to best suit your needs.

See also this answer who talk about the core structure of express between version 3 & 4, with the removing of external module.

Giverin answered 1/4, 2019 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.