Express和hapi如何相互比较? [英] How do Express and hapi compare to each other?

查看:128
本文介绍了Express和hapi如何相互比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Web应用程序设计和开发的角度来看,Express和Hapi如何相互比较?对于基本的例子,我们有兴趣了解整体应用程序结构的关键差异。



例如,据我所知,Hapi使用不同的路由机制,不考虑注册订单,可以做更快的查找,但与Express相比有限。还有其他重要的区别吗?



还有一个文章关于选择Hapi(over Express)开发新的npmjs.com网站,本文指出Hapi的插件系统意味着我们可以以允许微服务器的方式隔离应用程序的不同方面和服务未来,另一方面,Express需要更多的配置来获得相同的功能,这是什么意思?

解决方案

这是一个很大的问题,需要一个很长的答案才能完成,所以我只想解决一个最重要的区别。抱歉,这仍然是一个漫长的答案。



他们是如何相似的?



你是绝对正确的,当你说:


对于基本示例,他们看起来类似


这两个框架都解决了同样的基本问题:为节点中构建HTTP服务器提供了方便的API。也就是说,比使用较低级别的本机 http 模块。 http 模块可以做所有我们想要的工作,但是编写应用程序是很麻烦的。



为了实现这一点,在高级Web框架中长期使用概念:路由,处理程序,插件,认证模块。他们可能不一定有相同的名称,但大致相同。



大多数基本示例如下所示:




  • 创建路线

  • 请求路由时运行功能,准备响应

  • 回复请求



Express:

  app.get('/',function(req,res){

getSomeValue(function(obj){

res.json({an:'object '});
});
});

hapi:

 code> server.route({
method:'GET',
path:'/',
handler:function(request,reply){

getSomeValue(function(obj){

reply(obj);
});
}
});

差异在这里不完全是突破性的?那么为什么选择一个呢?



它们有什么不同?



简单的答案是hapi是还有更多的开箱即用。当您只是看看上面的简单示例时可能不清楚。其实这是有意的。简单的案例保持简单。所以让我们来看看一些很大的区别:



哲学



Express的意图非常小。通过在 http 之上给您一个小灰尘的小型API,您仍然非常依赖于添加其他功能。如果您想阅读传入请求的主体(相当常见的任务),则需要安装单独的模块。如果您希望将各种内容类型发送到该路由,您还需要检查内容类型标头来检查它是哪一个并相应地解析(表单通常使用单独的模块。



hapi具有丰富的功能集,通常通过配置选项公开,而不需要代码被写例如,如果我们要确保在处理程序运行之前,一个请求体(payload)完全读入内存并适当地解析(自动基于内容类型),那么这只是一个简单的选项

  server.route {
config:{
payload:{
output:'data',
parse:true
}
},
method:' GET',
路径:'/',
处理程序:function(request,reply){

reply(request.payload);
}
});



功能



您只需要比较这两个项目的API文档可以看出,hapi提供了更大的功能集。



hapi包含一些内置的Express功能,知道):





可扩展性模块化



hapi和Express以相当不同的方式介绍可扩展性。使用Express,您可以中间件功能。中间件功能类似于过滤器,您可以在打开处理程序之前,将堆栈和所有请求运行通过它们。



hapi具有请求生命周期,并提供扩展点,它们与中间件功能相当,但在请求生命周期中存在几个定义的点。



沃尔玛建立hapi并停止使用的原因之一Express对于将Express应用程序分割成不同的部分有多困难,并有不同的团队成员安全地在他们的大块上工作感到沮丧。为此,他们在hapi中创建了插件系统



一个插件就像一个子应用程序,您可以在hapi应用程序中完成所有操作,添加路线,扩展点等。在插件中,您可以确定您不会破坏应用程序的另一部分,因为路由的注册顺序并不重要,您不能创建冲突的路由。然后,您可以将此插件组合到服务器中并进行部署。



生态系统



因为快递给你这么少开箱即用,您需要在外部添加任何项目。在使用hapi时,很多时候,您需要的功能是内置的,还是由核心团队创建的模块。



最小的声音很棒。但是,如果你正在建立一个严肃的制作应用程序,那么你最终将需要所有这些东西。



安全性



hapi是由沃尔玛的团队设计的,以运行黑色星期五的流量,所以安全性和稳定性一直是最受关注的。由于这个原因,框架做了很多事情,例如限制进入的有效载荷大小,以防止耗尽你的进程内存。它还具有诸如最大事件循环延迟,最大使用的RSS内存和v8堆最大大小等选项,超过此选项,您的服务器将以503超时而不是仅仅崩溃来响应。



摘要



自己评估他们。想想你的需求,哪两个解决你最大的问题。在两个社区(IRC,Gitter,Github)中下降,看看你喜欢什么。不要只是说我的话。和快乐的黑客!






免责声明:作为关于hapi的书,上面大部分是我个人的意见。


From web application design and development point of view, how do Express and Hapi compare to each other? For basic examples they seem similar, however I'm interested to learn more about key differences in overall application structure.

For example, as far as I have learned, Hapi uses a different routing mechanism which does not take registration order into account, can do faster lookups, but is limited comparing to Express. Are there other important differences?

There is also an article about choosing Hapi (over Express) for developing the new npmjs.com website, this article states that "Hapi’s plugin system means that we can isolate different facets and services of the application in ways that would allow for microservices in the future. Express, on the other hand, requires a bit more configuration to get the same functionality", what does it exactly mean?

解决方案

This is a big question and requires a long answer to be complete, so I'll just address a subset of the most important differences. Apologies that it's still a lengthy answer.

How are they similar?

You're absolutely right when you say:

For basic examples they seem similar

Both frameworks are solving the same basic problem: Providing a convenient API for building HTTP servers in node. That is to say, more convenient than using the lower-level native http module alone. The http module can do everything we want but it's tedious to write applications with.

To achieve this, they both use concepts that have been around in high level web frameworks for a long time: routing, handlers, plugins, authentication modules. They might not have always had the same names but they're roughly equivalent.

Most of the basic examples look something like this:

  • Create a route
  • Run a function when the route is requested, preparing the response
  • Respond to the request

Express:

app.get('/', function (req, res) {

    getSomeValue(function (obj) {

        res.json({an: 'object'});
    });
});

hapi:

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        getSomeValue(function (obj) {

            reply(obj);
        });
    }
});

The difference is not exactly groundbreaking here right? So why choose one over the other?

How are they different?

The simple answer is hapi is a lot more and it does a lot more out-of-the-box. That might not be clear when you just look at the simple example from above. In fact, this is intentional. The simple cases are kept simple. So let's examine some of the big differences:

Philosophy

Express is intended to be very minimal. By giving you a small API with just a thin dusting on top of http, you're still very much on your own in terms of adding additional functionality. If you want to read the body of an incoming request (quite a common task), you need to install a separate module. If you're expecting various content-types to be sent to that route, you also need to check the Content-type header to check which it is and parse it accordingly (form-data vs JSON vs multi-part for example), often using separate modules.

hapi has a rich feature set, often exposed through configuration options, rather than requiring code to be written. For instance, if we want to make sure a request body (payload) is fully read into memory and appropriately parsed (automatically based on content-type) before the handler is ran, it's just a simple option:

server.route({
    config: {
        payload: {
            output: 'data',
            parse: true
        }
    },
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        reply(request.payload);
    }
});

Features

You only need to compare the API documentation on both projects to see that hapi offers a bigger feature set.

hapi includes some of the following features built-in that Express does not (as far as I know):

Extensibility & modularity

hapi and Express go about extensibility in quite a different way. With Express, you have middleware functions. Middleware functions are kind of like filters that you stack up and all requests run through them before hitting your handler.

hapi has the request lifecycle and offers extension points, which are comparable to middleware functions but exist a several defined points in the request lifecycle.

One of the reasons that Walmart built hapi and stopped using Express was a frustration with how difficult it was to split a Express app into separate parts, and have different team members work safely on their chunk. For this reason they created the plugin system in hapi.

A plugin is like a sub-application, you can do everything you can in a hapi app, add routes, extensions points etc. In a plugin you can be sure that you're not breaking another part of the application, because the order of registrations for routes doesn't matter and you can't create conflicting routes. You can then combine this plugins into a server and deploy it.

Ecosystem

Because Express gives you so little out of the box, you need to look outside when you need to add anything to your project. A lot of the times when working with hapi, the feature that you need is either built-in or there's a module created by the core team.

Minimal sounds great. But if you're building a serious production app, the chances are you're going to need all of this stuff eventually.

Security

hapi was designed by the team at Walmart to run Black Friday traffic so security and stability have always been a top concern. For this reason the framework does a lot of things extra such as limiting incoming payload size to prevent exhausting your process memory. It also has options for things like max event loop delay, max RSS memory used and max size of the v8 heap, beyond which your server will respond with a 503 timeout rather than just crashing.

Summary

Evaluate them both yourself. Think about your needs and which of the two addresses your biggest concerns. Have a dip in the two communities (IRC, Gitter, Github), see which you prefer. Don't just take my word. And happy hacking!


DISCLAIMER: I am biased as the author of a book on hapi and the above is largely my personal opinion.

这篇关于Express和hapi如何相互比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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