node.js 为什么要使用 Restify?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17589178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why should I use Restify?
提问by mithunsatheesh
I had the requirement to build up a REST API in node.js and was looking for a more light-weight framework than express.js which probably avoids the unwanted features and would act like a custom-built framework for building REST APIs. Restify from its intro is recommended for the same case.
我需要在 node.js 中构建 REST API,并且正在寻找比 express.js 更轻量级的框架,它可能避免不需要的功能,并且可以像构建 REST API 的定制框架一样工作。对于相同的情况,建议从其介绍中恢复。
Reading Why use restify and not express?seemed like restify is a good choice.
阅读为什么使用restify而不是express?似乎restify是一个不错的选择。
But the surprise came when I tried out both with a load.
但是当我用负载尝试两者时,惊喜来了。
I made a sample REST API on Restify and flooded it with 1000 requests per second. Surprise to me the route started not responding after a while. The same app built on express.js handled all.
我在 Restify 上制作了一个示例 REST API,并以每秒 1000 个请求淹没它。令我惊讶的是,路线在一段时间后开始没有响应。构建在 express.js 上的同一个应用程序处理了所有问题。
I am currently applying the load to API via
我目前正在通过
var FnPush = setInterval(function() {
for(i=0;i<1000;i++)
SendMsg(makeMsg(i));
}, 1000);
function SendMsg(msg) {
var post_data = querystring.stringify(msg);
var post_options = {
host: target.host,
port: target.port,
path: target.path,
agent: false,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length,
"connection": "close"
}
};
var post_req = http.request(post_options, function(res) {});
post_req.write(post_data);
post_req.on('error', function(e) {
});
post_req.end();
}
Does the results I have got seem sensible? And if so is express more efficient than restify in this scenario? Or is there any error in the way I tested them out?
我得到的结果是否合理?如果是这样,在这种情况下 express 比 restify 更有效吗?或者我测试它们的方式有什么错误?
updated in response to comments
根据评论更新
behavior of restify
restify 的行为
when fed with a load of more than 1000 req.s it stopped processing in just 1 sec receiving till 1015 req.s and then doing nothing. ie. the counter i implemented for counting incoming requests stopped increment after 1015.
when fed with a load of even 100 reqs. per second it received till 1015 and gone non responsive after that.
当输入超过 1000 req.s 的负载时,它会在 1 秒内停止处理,直到 1015 req.s,然后什么都不做。IE。我为计算传入请求而实现的计数器在 1015 年后停止递增。
当以 100 个请求的负载喂食时。每秒接收到 1015 次,之后没有响应。
回答by Masum
Corrigendum: this information is now wrong, keep scrolling!
更正:此信息现在是错误的,请继续滚动!
there was an issue with the script causing the Restify test to be conducted on an unintended route. This caused the connection to be kept alive causing improved performance due to reduced overhead.
脚本存在问题,导致 Restify 测试在非预期路线上进行。这导致连接保持活动状态,从而由于开销减少而提高了性能。
This is 2015 and I think the situation has changed a lot since. Raygun.io has posted a recent benchmark comparing hapi, express and restify.
这是 2015 年,我认为自那以后情况发生了很大变化。Raygun.io 最近发布了一个比较 hapi、express 和 restify 的基准。
It says:
它说:
We also identified that Restify keeps connections alive which removes the overhead of creating a connection each time when getting called from the same client. To be fair, we have also tested Restify with the configuration flag of closing the connection. You'll see a substantial decrease in throughput in that scenario for obvious reasons.
我们还发现 Restify 使连接保持活动状态,这消除了每次从同一客户端调用时创建连接的开销。公平地说,我们还测试了带有关闭连接的配置标志的 Restify。由于显而易见的原因,您会看到在这种情况下吞吐量大幅下降。


Looks like Restify is a winner here for easier service deployments. Especially if you're building a service that receives lots of requests from the same clients and want to move quickly. You of course get a lot more bang for buck than naked Node since you have features like DTrace support baked in.
看起来 Restify 是这里的赢家,可以更轻松地部署服务。特别是如果您正在构建一个接收来自相同客户端的大量请求并希望快速移动的服务。你当然比裸 Node 获得更多的收益,因为你有像 DTrace 支持这样的功能。
回答by Puneet Singh
This is 2017and the latest performance test by Raygun.iocomparing hapi, express, restify and Koa.
这是2017 年和Raygun.io比较 hapi、express、restify 和 Koa的最新性能测试。
It shows that Koa is faster than other frameworks, but as this question is about express and restify, Express is faster than restify.
这表明 Koa 比其他框架更快,但是由于这个问题是关于 express 和 restify,所以Express 比 restify 快。
And it is written in the post
而且帖子里写的
This shows that indeed Restify is slower than reported in my initial test.
这表明 Restify 确实比我最初测试中报告的要慢。
回答by Eric Elliott
According to the Node Knockout description:
restify is a node.js module purpose built to create REST web services in Node. restify makes lots of the hard problems of building such a service, like versioning, error handling and content-negotiation easier. It also provides built in DTrace probes that you get for free to quickly find out where your application's performance problems lie. Lastly, it provides a robust client API that handles retry/backoff for you on failed connections, along with some other niceties.
restify 是一个 node.js 模块,专门用于在 Node 中创建 REST Web 服务。restify 使构建此类服务的许多难题变得更容易,例如版本控制、错误处理和内容协商。它还提供了内置的 DTrace 探针,您可以免费获得这些探针,以快速找出应用程序的性能问题所在。最后,它提供了一个强大的客户端 API,可以在连接失败时为您处理重试/退避,以及其他一些细节。
Performance issues and bugs can probably be fixed. Maybe that description will be adequate motivation.
可能可以修复性能问题和错误。也许这种描述将是足够的动力。
回答by craigwaterman
I ran into a similar problem benchmarking multiple frameworks on OS X via ab. Some of the stacks died, consistently, after around the 1000th request.
我在通过 ab 对 OS X 上的多个框架进行基准测试时遇到了类似的问题。在第 1000 次请求之后,一些堆栈始终消失。
I bumped the limit significantly, and the problem disappeared.
我大大突破了限制,问题就消失了。
You can you check your maxfiles is at with ulimit, (or launchctl limit< OS X only) and see what the maximum is.
您可以使用ulimit检查您的 maxfiles ,(或仅使用 launchctl limit< OS X)并查看最大值是多少。
Hope that helps.
希望有帮助。
回答by kushvarma
i was confused with express or restify or perfectAPI. even tried developing a module in all of them. the main requirement was to make a RESTapi. but finally ended up with express, tested my self with the request per second made on all the framework, the express gave better result than others. Though in some cases restify outshines express but express seams to win the race. I thumbs up for express. And yes i also came across locomotive js, some MVC framework build on top of express. If anyone looking for complete MVC app using express and jade, go for locomotive.
我对 express 或 restify 或 perfectAPI 感到困惑。甚至尝试在所有这些中开发一个模块。主要要求是制作一个RESTapi。但最终以 express 结束,用在所有框架上每秒发出的请求测试了我的自我,express 给出了比其他更好的结果。虽然在某些情况下,恢复优于快递,但快递接缝赢得了比赛。我为快递竖起大拇指。是的,我也遇到了 locomotive js,一些构建在 express 之上的 MVC 框架。如果有人正在寻找使用 express 和 jade 的完整 MVC 应用程序,请选择机车。

