185

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?

Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • 1
    See best **Explanation** at https://stackoverflow.com/a/36638353/984471 – Manohar Reddy Poreddy Nov 03 '19 at 11:18
  • 1
    It should be noted that `/bin/www` is specific to the `express-generator` tool. From the [express docs](https://expressjs.com/en/guide/migrating-4.html#app-gen): "Neither the `bin` directory nor the extensionless `www` file is mandatory for creating an Express app or starting the app." – zr0gravity7 Jan 11 '22 at 18:27

7 Answers7

162

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",
}
Andy
  • 5,287
  • 2
  • 41
  • 45
  • ... and I'm also having some difficulty making this work on various node / cloud hosting environment – Andy Apr 23 '14 at 15:59
  • 8
    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? – Blaszard Apr 25 '14 at 05:03
  • @Ved Where is the document for what you just said about "npm start"? I'd like to study it. Thank you! – Nicolas S.Xu May 03 '14 at 07:02
  • 1
    @NicolasS.Xu On the ExpressJS's Github repo https://github.com/visionmedia/express, scroll down to the Quick start section – Andy May 05 '14 at 07:51
  • @Ved But windows doesn't support shell script. So it seems it's not very friendly with windows? – victorwoo May 19 '14 at 13:43
  • @victorwoo Not sure what you mean, the scripts are just javascripts, runs perfectly fine on Windows. – Andy May 20 '14 at 03:54
  • @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? – victorwoo May 20 '14 at 07:55
  • @victorwoo 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. – Andy May 21 '14 at 03:39
  • 7
    @Ved why "bin", though? I associate that with binary executables. – regularmike Nov 14 '14 at 15:56
  • 2
    @regularmike I guess, it could also means 'executable' scripts (like in the linux's environment) – Andy Nov 19 '14 at 06:07
  • 2
    @Ved yes, seems like a long time ago it evolved to mean anything executable. Common in Python, Perl, and unix/linux in general. – regularmike Nov 19 '14 at 14:45
  • you are super saver !! thanks alot :) heroku told everything but not that – A.T. Jul 10 '15 at 20:34
  • So how can I integrate it to CoffeeScript? – Zeck Jul 13 '15 at 12:49
  • 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? – Tenz Jan 10 '17 at 18:19
  • 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.js` – Jim Factor Apr 17 '18 at 23:58
  • @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`”? – BBaysinger May 16 '18 at 00:19
  • 1
    @BBaysinger it means what you think it means – Andy May 16 '18 at 03:25
  • 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? – coder101 May 03 '19 at 21:11
  • 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.1` – Rajan Sep 07 '19 at 06:01
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.

4dgaurav
  • 11,360
  • 4
  • 32
  • 59
  • can you please help me answer this question? Thank you! – Nicolas S.Xu May 03 '14 at 07:05
  • 11
    Why is it called bin? It doesn't contain binaries . . . – Kinnard Hockenhull Jul 30 '17 at 18:10
  • 1
    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 explanation](https://unix.stackexchange.com/a/370973) – nvidot Oct 21 '21 at 11:29
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.

Kapil Raghuwanshi
  • 867
  • 1
  • 13
  • 22
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.

Max Ma
  • 1,060
  • 9
  • 15
  • 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 https://gist.github.com/rseroter/79ad73f5e6e20b029f765ca8b34f4fad – Bishoy Hanna Jul 15 '19 at 07:50
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.

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
-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

timtch
  • 114
  • 2
  • 4
-2

On Windows, use this command:

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

  • 7
    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. – Jake Feb 14 '19 at 00:08