## Koa
### Comparisons
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.
### Features
##### Context
A Koa Context encapsulates node's `request` and `response` objects into a single object which provides many helpful methods for writing web applications and APIs. These operations are used so frequently in HTTP server development that they are added at this level instead of a higher level framework, which would force middleware to re-implement this common functionality.
##### Requests
A Koa Request object is an abstraction on top of node's vanilla request object, providing additional functionality that is useful for every day HTTP server development.
##### Responses
A Koa Response object is an abstraction on top of node's vanilla response object, providing additional functionality that is useful for every day HTTP server development.
##### Centralized Error Handling
By default outputs all errors to stderr unless app.silent is true. The default error handler also won't output errors when err.status is 404 or err.expose is true. To perform custom error-handling logic such as centralized logging you can add an "error" event listener
```javascript
app.on('error' => (err, ctx) => {
log.error('server error', err, ctx)
});
```
##### Cascading
Express `next()` is called at the end of middleware call and gives the control to next middleware. Previous middleware is unaware of how next middleware is executed. Middlewares can interact in one way, the last middleware is usually responsible for sending a request.
With async functions we can achieve "true" middleware. Contrasting other implementations which simply passes control through series of functions until one returns, Koa invoke "downstream", then control flows back "upstream".
Example:
```javascript
const Koa = require('koa');
const app = new Koa();
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});
// x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
// response
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
```
Since the response isn't sent explicitly like res.send() but after middleware stack execution is finished, it's possible to modify it in other middlewares (this can be used for good or bad)
Info gathered from:
- https://stackoverflow.com/questions/53039365/how-is-koa-middleware-different-from-express-middleware
- https://koajs.com/
- https://stackoverflow.com/questions/23140968/what-are-the-differences-between-koa-and-express-4-0
## Restify
### Comparisons
Restify isn't a complete replacement for Express. Everything Restify can do, Express can do as well. But the reverse is not true.
Express' use case is targeted at browser applications and contains a lot of functionality, such as templating and rendering, to support that. Restify does not.
Restify exists to let you build "strict" API services that are maintanable and observable.
Restify is targeted at the specific problems associated with creating a REST API. Express, on the other hand, gives you tools to address a wide variety of traditional web app problems (views, templating, etc) that just aren't necessary if the only thing you're building is an API.
### Features
##### Production ready
restify is used by some of the industry's most respected companies to power some of the largest deployments of Node.js on planet Earth.
##### Debuggable
Running at scale requires the ability to trace problems back to their origin by separating noise from signal. restify is built from the ground up with post-mortem debugging in mind.
##### Semantically correct
Staying true to the spec is one of the foremost goals of the project. You will see references to RFCs littered throughout GitHub issues and the codebase.
##### Dtrace
One of the coolest features of restify is that it automatically creates DTrace probes for you whenever you add a new route/handler. To use DTrace you need to pass dtrace option to the server `restify.createServer({ dtrace: true })`
Info gathered from:
- http://restify.com/
- https://www.quora.com/What-are-the-pros-and-cons-of-replacing-Express-with-Restify-in-a-MEAN-javascript-stack
- https://www.quora.com/What-makes-Restify-a-better-Node-js-module-than-express
- https://www.quora.com/What-are-the-pros-and-cons-of-replacing-Express-with-Restify-in-a-MEAN-javascript-stack