I am writing a REST API with Express for a while now. I have been reading into Koa.js and it sounds very interesting, but I can't seem to figure out how to write proper ES6 features with Koa.js. I am trying to make a structured application and this is what I have now:
note: I am using the koa-route package,
let koa = require('koa');
let route = require('koa-route');
let app = koa();
class Routes {
example() {
return function* () {
this.body = 'hello world';
}
}
}
class Server {
constructor(port) {
this.port = port;
}
addGetRequest(url, func) {
app.use(route.get('/', func());
}
listen() {
app.listen(this.port);
}
}
const port = 8008;
let routes = new Routes();
let server = new Server(port);
server.addGetRequest('/', routes.example);
server.listen();
It works, but it looks and feels clunky. Is there a better way to do this?