Building Performant API's with Koa.js
Tryshchenko Oleksandr @ DataArt
tryshchenko.com || otry.eu
Agenda
"Hey, guys, let's try koa?"
Koa
Express
Overall
Overall
Contributions
What is Koa
Middleware Core | Routing | Template Engine | HTTP Utilities | |
Koa | + | - | - | - |
Express | + | + | + | + |
Connect | + | - | - | - |
So, why Koa? 💡
Event Loop
console.log(1);
setTimeout(() => console.log(2), 0);
console.log(3);
// 1
// 3
// 2
Async
const chownCallback = () => ...;
fs.chown(path, uid, gid, chownCallback);
Sync
fs.chownSync(path, uid, gid);
/** some logic after chown */
Async
Async / Await
Async / Await
fetch('https://someapi.somedomain/something')
.then(response => response.json())
.then(json => console.log(json));
Async / Await
const res = await fetch('https://someapi.somedomain/something');
const json = await res.json();
console.log(json);
Middlewares
Middlewares
Middleware Architecture
Scaffold
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello Hannover!';
});
app.listen(3000);
Middleware
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
const res = await nextElections();
ctx.body = res;
...
await next();
});
app.listen(3000);
Writing a middleware
(ctx, next) => {}
What to take care about
Libs Quality
Express?
Performance isn't about tools...
Performance: Databases
Performance: HTTP
Peformance: Queues
Performance: Caching
Caching
@TODO write something about how freaking terrible situation is in koa ecosystem
Error Handling
What do I use
Security
Handpicked
Send me a Pigeon