What does "./bin/www" do in Express 4.x?
Asked Answered
A

7

188

I just started to learn about Express 4.0 in my Node.js app, and I found that it generated ./bin/www file, on which only the application server and port settings are written and everything others like middleware and routing is defined in ./app.js file.

However, I'm not sure what this ./bin/www does. I've used Express 3.x and I have always defined server and port settings as well as routing and middleware on the identical ./app.js file, and launched my node app with node app.js. So what's the point of using the ./bin/www? Does it only separate the server and port definition from others?

Right now, when I create the package using express-generator, the package.json includes the following definition:

"scripts": {
    "start": "node ./bin/www"
}

However, I wonder whether I should launch my app using node ./bin/www, or npm start. Which command should I run to start my app?

And also, when I deploy my app to heroku, what should I write in the Procfile file? Is web: node app.js enough?

Abecedarian answered 19/4, 2014 at 12:15 Comment(2)
See best Explanation at https://mcmap.net/q/137146/-learning-node-js-express-js-what-39-s-the-deal-with-bin-wwwGarges
It should be noted that /bin/www is specific to the express-generator tool. From the express docs: "Neither the bin directory nor the extensionless www file is mandatory for creating an Express app or starting the app."Summit
S
164

In Express 3.0, you normally would use app.configure() (or app.use()) to set up the required middleware you need. Those middleware you specified are bundled together with Express 3.0.

Example:

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.compress());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());

In Express 4.0 however, all middleware have been removed so that they can be maintained and updated independently from the core Express (except the static middleware), thus they need to be called separately (what you see in app.js).

The bin/ directory serves as a location where you can define your various startup scripts. The www is an example to start the express app as a web server.

Ultimately, you could have different scripts like test, stop, or restart, etc. Having this structure allows you to have different startup configurations, without cramming everything into app.js.

The correct way to start your Express app is:

npm start

To deploy an Express 4.x app to Heroku, add this to your Procfile:

web: npm start

Or if you can just use the start script in your package.json, heroku will automatically uses that, read more here

"scripts": {
    "start": "node ./bin/www",
}
Stratagem answered 23/4, 2014 at 15:57 Comment(19)
... and I'm also having some difficulty making this work on various node / cloud hosting environmentStratagem
Thanks. With your excellent explanation on the last two paragraphs, I finally got what's the point of using www. I'm not sure why it's named that way though - maybe named after World Wide Web?Abecedarian
@Ved Where is the document for what you just said about "npm start"? I'd like to study it. Thank you!Saith
@NicolasS.Xu On the ExpressJS's Github repo github.com/visionmedia/express, scroll down to the Quick start sectionStratagem
@Ved But windows doesn't support shell script. So it seems it's not very friendly with windows?Asarum
@Asarum Not sure what you mean, the scripts are just javascripts, runs perfectly fine on Windows.Stratagem
@Ved I mean "#!/usr/bin/env node", it seems a linux like path and can't cross platform. It also make JSLint reports error. I think if it's better to separate shell script part and JavaScript part into individual files?Asarum
@Asarum Yes, if your scripts are platform dependent. I personally think that might be a task for Grunt.js. As for the path, it mostly works on Windows where most of the time developer already installed Git (msysgit), so that is a valid path even on Windows.Stratagem
@Ved why "bin", though? I associate that with binary executables.Blackjack
@Blackjack I guess, it could also means 'executable' scripts (like in the linux's environment)Stratagem
@Ved yes, seems like a long time ago it evolved to mean anything executable. Common in Python, Perl, and unix/linux in general.Blackjack
you are super saver !! thanks alot :) heroku told everything but not thatAbsorber
So how can I integrate it to CoffeeScript?Too
Hi. I think the bin folder has prevented me from using websocket. Everytime I am trying to do websocket, it doesnt work on herokuapp. It works for localhost only. Any idea how to start websocket and make it works fine on heroku using the express 4?Gossipry
not sure if this is specific to nodemon or node 8.11.1 issue but I had to change mine to nodemon ./bin/ww.js including the .js extension otherwise it was trying to run as www index.jsClaudineclaudio
@Andy, "what you see in app.js"; I'm not sure what this statement means. Perhaps you meant “from what you see in app.js”?Bunkhouse
@Bunkhouse it means what you think it meansStratagem
New to Express. So what you're saying is that we should leave bin/www alone and make any changes to app.js (including importing any new dependencies that we install)? Also, the way npm start in package.json seems to execute bin/www. How does that in turn execute app.js?Teaching
Interestingly npm start does nothing for me. package.json ``` "scripts": { "start": "node ./bin/www", }, ``` Yet, if I do node ./bin/www directly it will work and startup the server. :think: * npm --version: 6.10.3 * node --version: v12.9.1 * express --version: 4.16.1Bracket
T
12

Node apps like the Express 3.x use non-standard startup files app.js, but it's the wrong file to run.

package.json has

   "scripts": {
     "start": "node ./bin/www"
   }

which states the startup command line. It’s non-trivial because that potentially contains a full command line, and not just a path to the starter file.

Tarton answered 19/4, 2014 at 15:32 Comment(3)
can you please help me answer this question? Thank you!Saith
Why is it called bin? It doesn't contain binaries . . .Stanford
For those wondering like Kinnard, this has probably to do with the general use of 'bin' for all things "executable" in unix like OSes and for the reason why unix called it 'bin' here is an explanationAlpine
E
12

All the above have answered well. But in case if you want to use node app.js only like Express 3.* versions. You can follow below:

Because the app.js file that was generated by the Express 4 generator is now a Node.js module, it can no longer be started independently as an app (unless you modify the code). The module must be loaded in a Node.js file and started via the Node.js file. The Node.js file is ./bin/www in this case. For more info, visit the official documentation.

Neither the bin directory nor the extensionless www file is mandatory for creating an Express app or starting the app. They are just suggestions made by the generator, so feel free to modify them to suit your needs.

To get rid of the www directory and keep things the “Express 3 way”, delete the line that says module.exports = app; at the end of the app.js file, then paste the following code in its place:

app.set('port', process.env.PORT || 3000)

app.listen(app.get('port'), () => {
  console.log(`Express server listening on port ${app.get('port')}`);
})

Next, change "start": "node ./bin/www" in the package.json file. Since, you have now moved the functionality of ./bin/www back to app.js. Now, start using "start": "node app.js" for running your express app.

Eburnation answered 30/5, 2020 at 7:22 Comment(0)
I
3

if you are using express-generator, just look at your local file, there is www file inside of the ./bin. So when you run node ./bin/www, node.js will execute the code at www file. Nothing fancy.

Indent answered 2/9, 2017 at 4:51 Comment(1)
and if you are deploying to Azure app service, you have to create web.config and replace all index.js with bin/www , similar to that document gist.github.com/rseroter/79ad73f5e6e20b029f765ca8b34f4fadGonion
S
0

From the express docs:

[...] The actual command that starts the app is node ./bin/www [in Express 4], which used to be node app.js in Express 3. Because the app.js file that was generated by the Express 4 generator is now a Node.js module, it can no longer be started independently as an app (unless you modify the code). The module must be loaded in a Node.js file and started via the Node.js file. The Node.js file is ./bin/www in this case.

Summit answered 11/1, 2022 at 18:29 Comment(0)
A
-1

put this to Procfile web: node ./bin/www and check if it works with foreman start. The app should be available on port 5000

Arborvitae answered 11/5, 2014 at 21:34 Comment(0)
C
-2

On Windows, use this command:

set DEBUG=myapp:* & npm start Then load http://localhost:3000/ in your browser to access the app.

Crossbill answered 13/2, 2019 at 23:6 Comment(1)
Welcome to SO! I don't think this really answers the question at hand. Please re-read the question to fully understand all that the Original Poster is asking.Mandamandaean

© 2022 - 2024 — McMap. All rights reserved.