简体   繁体   English

MEAN Stack应用程序结构

[英]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. 我使用express-generator npm为MEAN Stack应用程序创建了样板,效果很好,但是我不明白几个文件的用途。

For example: 例如:

The package.json contained the following code: package.json包含以下代码:

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

The app contained a folder called bin which contained a file called www which contained the code below: 该应用程序包含一个名为bin的文件夹,其中包含一个名为www的文件,其中包含以下代码:

#!/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: 现在我不确定这是什么目的,因为我删除了上面的所有代码,并在服务器所在的app.js文件中添加了以下几行:

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? 当我们仅用2行替换它时,为什么需要它呢? It just seems very confusing and unnecessary. 这似乎非常令人困惑和不必要。

Inside of your package.json file, the line "script":{"start": "node ./bin/www"} tells node where to go to start your application. 在package.json文件中,行"script":{"start": "node ./bin/www"}告诉节点启动应用程序的位置。

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); 该行: server.on('error', onError); creates an event listener that is bound to the onError method. 创建绑定到onError方法的事件侦听器。 When an error is detected, the onError() method is called and executed, throwing the error. 当检测到错误时,将调用onError()方法并执行该方法,从而引发错误。

Similarly, the server.on('listening', onListening); 同样, server.on('listening', onListening); creates an event listener that is bound to the onListening method. 创建绑定到onListening方法的事件侦听器。 When the server is actually listening on a normalized port the onListening() method is called and executed. 当服务器实际在规范化端口上侦听时,将调用并执行onListening()方法。

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. 如果您的应用程序没有启动或崩溃,但是您没有内置的错误处理或报告功能,则有时可能会使查找问题变得很繁琐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM