javascript - MEAN Stack App Structure -


i used express-generator npm create boilerplate mean stack app , worked didn't understand purpose of few files.

for example:

the package.json contained following code:

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

the app contained folder called bin contained file called www contained 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 friendly messages       switch (error.code) {       case 'eacces':       console.error(bind + ' requires elevated privileges');       process.exit(1);       break;       case 'eaddrinuse':       console.error(bind + ' 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 purpose of because removed code above , added following lines in app.js file server is:

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

by replacing code 2 lines able run server , display view using routes. thats how have been developing node/express apps while , have worked fine.

can explain point of code because not sure does? , why need when can replace 2 lines? seems confusing , unnecessary.

inside of package.json file, line "script":{"start": "node ./bin/www"} tells node go start application.

the code removed includes error checking , validates server listening on normalized port , running.

the line: server.on('error', onerror); creates event listener bound onerror method. when error detected, onerror() method called , executed, throwing error.

similarly, server.on('listening', onlistening); creates event listener bound onlistening method. when server listening on normalized port onlistening() method called , executed.

the big difference between generated code , code, provides error handling, while yours not. error-handling absolutely essential , should not removed if intend push project live environment.

it provides way application give better error information (aiding in problem resolution/debugging), , handle problems without falling apart. if application not start or crashes, have not built-in error-handling or reporting, can make finding problem quite tedious @ times.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -