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

Javascriptnode.jsHerokuExpress

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 3 - Javascript

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 4 - Javascript

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.

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

Solution 7 - Javascript

On Windows, use this command:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlaszardView Question on Stackoverflow
Solution 1 - JavascriptAndyView Answer on Stackoverflow
Solution 2 - Javascript4dgauravView Answer on Stackoverflow
Solution 3 - JavascriptKapil RaghuwanshiView Answer on Stackoverflow
Solution 4 - JavascriptMax MaView Answer on Stackoverflow
Solution 5 - Javascriptzr0gravity7View Answer on Stackoverflow
Solution 6 - JavascripttimtchView Answer on Stackoverflow
Solution 7 - Javascriptuser11059213View Answer on Stackoverflow