NodeJS域名和expressjs [英] NodeJS Domains and expressjs

查看:120
本文介绍了NodeJS域名和expressjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在计划创建一个相当简单(但大型的)ExpressJS项目,它响应REST请求,并且只读DB响应。我已经阅读了关于NodeJS域的捕获错误,但是我不清楚我需要这个项目。我的测试似乎表明,即使在最糟糕的未捕获的投掷和崩溃中,只有单个REST请求失败,而不是整个服务器。



我是正确的吗? NodeJS域API: http://nodejs.org/api/domain.html#domain_class_domain



(显然我问的原因是域功能在项目结束后很难实现)。



如果不需要,本文对于NODEJS for Express的想法给我留下了深刻的印象:(如果不需要,我不会使代码复杂化)。



Node.js - 域速递请求,另一个域内

解决方案

使用此模块,获取域名的工作量少得多: https://github.com/brianc/node-domain-middleware



除此之外,虽然只有个别请求可能会失败,但e有时可能会让您的服务器处于恶劣状态(泄漏的资源就是一个例子),并且在这一点上重新启动服务器是很好的形式。如果Express的内置错误处理程序负责处理,那么您将无法自行关闭服务器。因此,有一个原因,这是很好的拥有域名,这样你可以有一个很好的一点,你的代码来捕获。上述中间件在您的主流中将该错误处理代码拖出并保持清洁。



forky ,事情变得更好。


Newbie to ExpressJS question:

I am planning on creating a fairly simple (but large) ExpressJS project that responds to REST requests and does read-only DB responses. I have read up about NodeJS domains for capturing errors, but it is not clear to me that I need that for this project. My testing seems to indicate that even in the worst uncaught throws and crashes, only the individual REST request fails, and not the whole server.

Am I correct? NodeJS Domain API: http://nodejs.org/api/domain.html#domain_class_domain

(Obviously the reason I ask is that domain functionality is hard to implement after-the-fact at the end of the project).

This article left me with cold feelings to the idea of NODEJS for Express if it is not needed: (I don't complicate my code if not needed).

Node.js - Domain per Express request, inside another domain

解决方案

Getting domains working is a lot less work with this module: https://github.com/brianc/node-domain-middleware.

Other than that, while it's true that only an individual request may fail, the errors can sometimes leave your server in a bad state (leaked resources are one example), and it's considered good form to restart the server at that point. If Express's built-in error handler takes care of that, you won't be able to shut down your server yourself. Thus, one reason why it's nice to have the domain, so that you can have a nice point in your code to capture that. The above middleware does a lot in pulling that error-handling code out of your main flow and keeping it clean.

Combined with forky, things get even better.

这篇关于NodeJS域名和expressjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆