<legend id='OmKEy'><style id='OmKEy'><dir id='OmKEy'><q id='OmKEy'></q></dir></style></legend>

    1. <small id='OmKEy'></small><noframes id='OmKEy'>

        <i id='OmKEy'><tr id='OmKEy'><dt id='OmKEy'><q id='OmKEy'><span id='OmKEy'><b id='OmKEy'><form id='OmKEy'><ins id='OmKEy'></ins><ul id='OmKEy'></ul><sub id='OmKEy'></sub></form><legend id='OmKEy'></legend><bdo id='OmKEy'><pre id='OmKEy'><center id='OmKEy'></center></pre></bdo></b><th id='OmKEy'></th></span></q></dt></tr></i><div id='OmKEy'><tfoot id='OmKEy'></tfoot><dl id='OmKEy'><fieldset id='OmKEy'></fieldset></dl></div>
        <tfoot id='OmKEy'></tfoot>

          <bdo id='OmKEy'></bdo><ul id='OmKEy'></ul>
      1. Node.js Express 应用程序处理启动错误

        时间:2024-04-19
        • <bdo id='zw47b'></bdo><ul id='zw47b'></ul>
          <legend id='zw47b'><style id='zw47b'><dir id='zw47b'><q id='zw47b'></q></dir></style></legend>
        • <small id='zw47b'></small><noframes id='zw47b'>

              1. <tfoot id='zw47b'></tfoot>
                    <tbody id='zw47b'></tbody>

                • <i id='zw47b'><tr id='zw47b'><dt id='zw47b'><q id='zw47b'><span id='zw47b'><b id='zw47b'><form id='zw47b'><ins id='zw47b'></ins><ul id='zw47b'></ul><sub id='zw47b'></sub></form><legend id='zw47b'></legend><bdo id='zw47b'><pre id='zw47b'><center id='zw47b'></center></pre></bdo></b><th id='zw47b'></th></span></q></dt></tr></i><div id='zw47b'><tfoot id='zw47b'></tfoot><dl id='zw47b'><fieldset id='zw47b'></fieldset></dl></div>
                  本文介绍了Node.js Express 应用程序处理启动错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Node.js 和 Express 中有应用程序.我需要为它编写测试.我在处理 Express 应用程序错误时遇到问题.我发现这个 我如何捕捉 node.js/express 服务器错误,例如 EADDRINUSE?,但它对我不起作用,我不知道为什么.我想处理 expressApp.listen() 执行时可能发生的错误(EADDRINUSE、EACCES 等).

                  I have app in Node.js and Express. I need to write tests for it. I have a problem with handling Express app errors. I found this How do I catch node.js/express server errors like EADDRINUSE?, but it doesn't work for me, I don't know why. I want to handle errors, which can occured while expressApp.listen() is executing (EADDRINUSE, EACCES etc.).

                  express = require('express')
                  listener = express()
                  
                  #doesn't work for me
                  listener.on('uncaughtException', (err) ->
                    #do something
                  )
                  
                  #doesn't work too
                  listener.on("error", (err) ->
                    #do something
                  )
                  
                  #this works, but it caughts all errors in process, I want only in listener
                  process.on('uncaughtException', (err) ->
                    #do something
                  )
                  
                  listener.listen(80) #for example 80 to get error
                  

                  有什么想法吗?

                  推荐答案

                  首先,expressJS 不会抛出 uncaughtException 事件,进程会抛出,所以你的代码不起作用也就不足为奇了.

                  First off, expressJS does not throw the uncaughtException event, process does, so it's no surprise your code doesn't work.

                  所以改用:process.on('uncaughtException',handler).

                  接下来,expressJS 已经提供了一种标准的错误处理方法,即使用它为此目的提供的中间件函数,如下所示:

                  Next, expressJS already provides a standard means of error handling which is to use the middleware function it provides for this purpose, as in:

                  app.configure(function(){
                      app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
                  });
                  

                  此函数向客户端返回一条错误消息,带有可选的堆栈跟踪,并记录在 connectJS errorHandler.

                  This function returns an error message to the client, with optional stacktrace, and is documented at connectJS errorHandler.

                  (注意errorHandler其实是connectJS的一部分,只被expressJS暴露出来.)

                  (Note that errorHandler is actually part of connectJS and is only exposed by expressJS.)

                  如果现有 errorHandler 提供的行为不足以满足您的需求,则其来源位于 connectJS 的 errorHandler 中间件,可以轻松修改以满足您的需求.

                  If the behavior the existing errorHandler provides is not sufficient for your needs, its source is located at connectJS's errorHandler middleware and can be easily modified to suit your needs.

                  当然,与其直接修改这个函数,正确"的做法是创建自己的errorHandler,以connectJS版本为起点,如:

                  Of course, rather than modifying this function directly, the "correct" way to do this is to create your own errorHandler, using the connectJS version as a starting point, as in:

                  var myErrorHandler = function(err, req, res, next){
                      ...
                      // note, using the typical middleware pattern, we'd call next() here, but 
                      // since this handler is a "provider", i.e. it terminates the request, we 
                      // do not.
                  };
                  

                  并将其安装到 expressJS 中:

                  And install it into expressJS as:

                  app.configure(function(){
                      app.use(myErrorHandler);
                  });
                  

                  请参阅 Just Connect it, Already 了解 connectJS 的 filterprovider 中间件和 How To Write Middleware for Connect/Express 以获得编写良好的教程.

                  See Just Connect it, Already for an explanation of connectJS's idea of filter and provider middleware and How To Write Middleware for Connect/Express for a well-written tutorial.

                  您可能还会发现这些有用:

                  You might also find these useful:

                  • 如何处理node.js中的代码异常?

                  从 Node.JS 中的未捕获异常中恢复

                  最后,可以在 它自己的测试中找到有关测试 expressJS 的极好信息来源.

                  这篇关于Node.js Express 应用程序处理启动错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Backbone 和 Rails 关联:避免 JSON HashWithIndifferentAccess 错误 下一篇:如何将骨干路由器应用于完整路径,而不是哈希

                  相关文章

                  <small id='Ka36B'></small><noframes id='Ka36B'>

                • <legend id='Ka36B'><style id='Ka36B'><dir id='Ka36B'><q id='Ka36B'></q></dir></style></legend>
                  <tfoot id='Ka36B'></tfoot>
                  <i id='Ka36B'><tr id='Ka36B'><dt id='Ka36B'><q id='Ka36B'><span id='Ka36B'><b id='Ka36B'><form id='Ka36B'><ins id='Ka36B'></ins><ul id='Ka36B'></ul><sub id='Ka36B'></sub></form><legend id='Ka36B'></legend><bdo id='Ka36B'><pre id='Ka36B'><center id='Ka36B'></center></pre></bdo></b><th id='Ka36B'></th></span></q></dt></tr></i><div id='Ka36B'><tfoot id='Ka36B'></tfoot><dl id='Ka36B'><fieldset id='Ka36B'></fieldset></dl></div>
                    • <bdo id='Ka36B'></bdo><ul id='Ka36B'></ul>