1

I used Bitnami to install a MEAN Stack

I am trying to understand something. I see a lot of tutorials which will show you could like this

var express = require('express');
var app = express();

app.get('/', function (req, res) {
   res.send('Hello World');
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("Example app listening at http://%s:%s", host, port)

})

Now within the command line, if I run node bin/www as per what the Bitnami documentation states, I get the error

TypeError: app.set is not a function

Now a simple solution to this is to add the following to the end of that code

module.exports = app;

However, instead of doing node bin/www if I invoked the file directly e.g. node app.js, the code will work without the extra addition above.

So really I am wondering why this is the case? I see a lot of online tutorials which do not assign app to module.exports. What is the difference between the two?

Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
katie hudson
  • 2,765
  • 13
  • 50
  • 93
  • Possible duplicate of [What does "./bin/www" do in Express 4.x?](http://stackoverflow.com/questions/23169941/what-does-bin-www-do-in-express-4-x) – Fabio Antunes Mar 29 '16 at 16:12
  • From express-generator - https://github.com/expressjs/generator/blob/master/templates/js/app.js - there is export at the end of the file. And https://github.com/expressjs/generator/blob/master/templates/js/www require `app.js` so it has access to .set() method. Also you can found some discussion about `bin/www` here https://github.com/expressjs/generator/issues/25 – Krzysztof Safjanowski Mar 29 '16 at 16:13

0 Answers0