Search code examples
javascriptnode.jsexpressexpress-generator

Benefits of using bin files over .js in express-generator


If one wants to jump start a project in Node.js with express. one would use express-generator. After creating a new project your file tree will look like this

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug

One thing that stood out for me is that to run the app you need to do node bin/www or a predefined shortcut npm run. My question is why would one use the www the way it is and not add a .js extension and remove #!/usr/bin/env node from the top of the file? Are there any benefits of doing it this way or is it a personal preference?


Solution

  • Let's look at the first line of the bin/www file:

    #!/usr/bin/env node
    

    This shebang tells the *nix operating system how to interpret the file if you try to run it as a program.

    So this file can be started as a program. And in Linux traditionally executable files do not have an extension.