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

85,552

Solution 1

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",
}

Solution 2

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.

Solution 3

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.

Solution 4

if you are using express-generator, just look at your local file, ./bin, 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.

Share:
85,552

Related videos on Youtube

Blaszard
Author by

Blaszard

I'm here to gain knowledge and insights on a variety of fields I'm interested in. Specifically, Programming & Software Development (Python and R; no longer use Swift and JavaScript/node.js) Data Science, Machine Learning, AI, & statistics Travel (started in 2016) Language (普通话, français, español, italiano, русский, 한국어) Politics, Economics, and Finance Currently (in 2020), my primary interest is Korean and Russian😈 PS: I'm not a native-English speaker. If you find any errors in my grammar and expressions, don't hesitate to edit it. I'll appreciate it👨🏻‍💼

Updated on January 12, 2022

Comments

  • Blaszard
    Blaszard over 2 years

    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?

    • Manohar Reddy Poreddy
      Manohar Reddy Poreddy over 4 years
      See best Explanation at stackoverflow.com/a/36638353/984471
    • zr0gravity7
      zr0gravity7 over 2 years
      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."
  • Andy
    Andy about 10 years
    ... and I'm also having some difficulty making this work on various node / cloud hosting environment
  • Blaszard
    Blaszard about 10 years
    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?
  • Nicolas S.Xu
    Nicolas S.Xu about 10 years
    @Ved Where is the document for what you just said about "npm start"? I'd like to study it. Thank you!
  • Nicolas S.Xu
    Nicolas S.Xu about 10 years
    can you please help me answer this question? Thank you!
  • Andy
    Andy almost 10 years
    @NicolasS.Xu On the ExpressJS's Github repo github.com/visionmedia/express, scroll down to the Quick start section
  • victorwoo
    victorwoo almost 10 years
    @Ved But windows doesn't support shell script. So it seems it's not very friendly with windows?
  • Andy
    Andy almost 10 years
    @victorwoo Not sure what you mean, the scripts are just javascripts, runs perfectly fine on Windows.
  • victorwoo
    victorwoo almost 10 years
    @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?
  • Andy
    Andy almost 10 years
    @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.
  • regularmike
    regularmike over 9 years
    @Ved why "bin", though? I associate that with binary executables.
  • Andy
    Andy over 9 years
    @regularmike I guess, it could also means 'executable' scripts (like in the linux's environment)
  • regularmike
    regularmike over 9 years
    @Ved yes, seems like a long time ago it evolved to mean anything executable. Common in Python, Perl, and unix/linux in general.
  • A.T.
    A.T. almost 9 years
    you are super saver !! thanks alot :) heroku told everything but not that
  • Zeck
    Zeck almost 9 years
    So how can I integrate it to CoffeeScript?
  • Tenz
    Tenz over 7 years
    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?
  • Kinnard Hockenhull
    Kinnard Hockenhull almost 7 years
    Why is it called bin? It doesn't contain binaries . . .
  • Jim Factor
    Jim Factor about 6 years
    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
  • BBaysinger
    BBaysinger almost 6 years
    @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”?
  • Andy
    Andy almost 6 years
    @BBaysinger it means what you think it means
  • Jake
    Jake about 5 years
    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.
  • coder101
    coder101 about 5 years
    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?
  • Bishoy Hanna
    Bishoy Hanna almost 5 years
    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/79ad73f5e6e20b029f765ca8b34f4fad
  • Rajan
    Rajan over 4 years
    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
  • nvidot
    nvidot over 2 years
    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