koa-lite/docs/koa-vs-express.md

106 lines
4 KiB
Markdown
Raw Permalink Normal View History

2013-12-26 08:31:01 +00:00
THIS DOCUMENT IS IN PROGRESS. THIS PARAGRAPH SHALL BE REMOVED WHEN THIS DOCUMENT IS DONE.
2014-03-10 11:20:54 +00:00
# Koa vs Express
2013-12-26 08:31:01 +00:00
2014-03-10 11:20:54 +00:00
Philosophically, Koa aims to "fix and replace node", whereas Express "augments node".
Koa uses co to rid apps of callback hell and simplify error handling.
It exposes its own `this.request` and `this.response` objects instead of node's `req` and `res` objects.
Express, on the other hand, augments node's `req` and `res` objects with additional properties and methods
and includes many other "framework" features, such as routing and templating, which Koa does not.
Thus, Koa can be viewed as an abstraction of node.js's `http` modules, where as Express is an application framework for node.js.
2013-12-26 08:31:01 +00:00
2014-03-10 11:20:54 +00:00
| Feature | Koa | Express | Connect |
|------------------:|-----|---------|---------|
| Middleware Kernel | ✓ | ✓ | ✓ |
| Routing | | ✓ | |
| Templating | | ✓ | |
| Sending Files | | ✓ | |
| JSONP | | ✓ | |
Thus, if you'd like to be closer to node.js and traditional node.js-style coding, you probably want to stick to Connect/Express or similar frameworks.
If you want to dive into the land of generators, use Koa.
As result of this different philosophy is that traditional node.js "middleware", i.e. functions of the form `(req, res, next)`, are incompatible with Koa. Your application will essentially have to be rewritten from the ground, up.
2013-12-26 08:31:01 +00:00
## Does Koa replace Express?
It's more like Connect, but a lot of the Express goodies
were moved to the middleware level in Koa to help form
a stronger foundation. This makes middleware more enjoyable
and less error-prone to write, for the entire stack, not
just the end application code.
Typically many middleware would
re-implement similar features, or even worse incorrectly implement them,
when features like signed cookie secrets among others are typically application-specific,
not middleware specific.
## Does Koa replace Connect?
No, just a different take on similar functionality
now that generators allow us to write code with less
callbacks. Connect is equally capable, and some may still prefer it,
it's up to what you prefer.
## Why isn't Koa just Express 4.0?
Koa is a pretty large departure from what people know about Express,
the design is fundamentally much different, so the migration from
Express 3.0 to this Express 4.0 would effectively mean rewriting
the entire application, so we thought it would be more appropriate
to create a new library.
## Is Koa faster than Express?
Koa and Express have very similar benchmark results, both of which are more than enough for most applications. This is especially true for applications with high traffic running multiple instances, because you are scaling horizontally right?
2013-12-26 08:31:01 +00:00
The following results were produced on a Retina Macbook Pro, showing only the cost of running noop middleware.
2013-12-26 08:31:01 +00:00
| no. middleware | Koa | Express |
| --------------:| ------- | ------- |
| 1 | 8849.36 | 7805.19 |
| 5 | 8685.74 | 7707.15 |
| 10 | 8511.08 | 7475.08 |
| 15 | 8456.86 | 7261.73 |
| 20 | 8211.93 | 7012.07 |
| 30 | 8102.98 | 6672.76 |
| 50 | 7617.67 | 6255.92 |
| 100 | 6899.45 | 5000.98 |
2013-12-26 08:31:01 +00:00
## How is Koa different than Connect/Express?
### Generated-based control flow
Thanks to co.
No callback hell.
Better error handling through try/catch.
No need for domains.
2014-03-07 22:11:02 +00:00
### Koa is barebones
2013-12-26 08:31:01 +00:00
Unlike both Connect and Express, Koa does not include any middleware.
Unlike Express, routing is not provided.
Unlike Express, many convenience utilities are not provided. For example, sending files.
2014-03-07 22:11:02 +00:00
Koa is more modular.
2013-12-26 08:31:01 +00:00
2014-03-07 22:11:02 +00:00
### Koa relies less on middleware
2013-12-26 08:31:01 +00:00
For example, instead of a "body parsing" middleware, you would instead use a body parsing function.
### Koa abstracts node's request/response
Less hackery.
2014-03-07 22:11:02 +00:00
Better user experience.
2013-12-26 08:31:01 +00:00
Proper stream handling.