Search code examples
javascriptnode.jsexpressmean-stackexpress-generator

MEAN Stack App Structure


I used the express-generator npm to create a boilerplate for a MEAN Stack app and it worked perfectly but I didn't understand the purpose of a few files.

For example:

The package.json contained the following code:

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

The app contained a folder called bin which contained a file called www which contained the code below:

#!/usr/bin/env node

/**
 * Module dependencies.
 */

 var app = require('../app');
 var debug = require('debug')('myapp:server');
 var http = require('http');


  var port = normalizePort(process.env.PORT || '3000');
  app.set('port', port);

  var server = http.createServer(app);

  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);

  function normalizePort(val) {
     var port = parseInt(val, 10);

    if (isNaN(port)) {
         // named pipe
           return val;
     }

   if (port >= 0) {
       // port number
       return port;
      }

      return false;
    }

  function onError(error) {
     if (error.syscall !== 'listen') {
      throw error;
    }

   var bind = typeof port === 'string' ?
   'Pipe ' + port :
   'Port ' + port;

    // handle specific listen errors with friendly messages
      switch (error.code) {
      case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
      case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
      default:
      throw error;
    }
   }

 function onListening() {
   var addr = server.address();
   var bind = typeof addr === 'string' ?
     'pipe ' + addr :
     'port ' + addr.port;
      debug('Listening on ' + bind);
    }

Now Im not sure whats the purpose of this because I removed all this code above and added the following lines in my app.js file where my server is:

var port = process.env.PORT || 8080;
app.listen(port);
console.log("Listening on port " + port)

By replacing all that code with only two lines I was able to run the server and display a view using routes. Thats how I have been developing all my node/express apps for a while and they have worked fine.

Can anyone explain what was the point of all that code because I am not sure what it does? And why would be need it when we can simply replace it with 2 lines? It just seems very confusing and unnecessary.


Solution

  • Inside of your package.json file, the line "script":{"start": "node ./bin/www"} tells node where to go to start your application.

    The code that you removed includes error checking and validates that the server is listening on a normalized port and running.

    The line: server.on('error', onError); creates an event listener that is bound to the onError method. When an error is detected, the onError() method is called and executed, throwing the error.

    Similarly, the server.on('listening', onListening); creates an event listener that is bound to the onListening method. When the server is actually listening on a normalized port the onListening() method is called and executed.

    The big difference between the generated code and your code, is that it provides error handling, while yours does not. Error-handling is absolutely essential and should not be removed if you intend to push your project into a live environment.

    It provides a way for you application to give you better error information (aiding in problem resolution/debugging), and handle problems without completely falling apart. If your application does not start up or crashes, but you have not built-in error-handling or reporting, it can make finding the problem quite tedious at times.