A method that use koa in Rescript!

When I started using rescript to do development, I wanted to write a demo http server with koa, but I didn’t find some rescript bindings for koa, there was an express binding in community just.
So I wrote the rescript-koa & rescript-koa-router bindings, by the way I can learn rescript more deeply.
welcome to give me corrections & issues. :innocent:
This is an example:

open Router
open Koa

let app = Koa.koa()

let router = Router.router(None)

router->get("/login", (context, next) => {
    context.body = "hello"
})

app->use(router->routes())

let _ = app->listen(8080, _ => {Js.Console.log("server is start success!")})

:partying_face:

8 Likes

I know that nobody asked me, but I suggest using Fastify instead :new_moon_with_face:
Koa is good for todo apps, but for anything bigger it is a bad choice. Btw, Express is even worse.

1 Like

Thanks, I’ll try Fastify later :sunglasses:

1 Like

He’s secretly hoping you’d write bindings for fastify now haha.

1 Like

I’ll try in the future, I need to use it first :sob:

1 Like

You’ve exposed me :grin:

Actually I have them, but they are not in a state to be published.

1 Like

Care to expand on this a little? Just curious on why you think so

  1. The biggest reason is the middleware architecture that I consider an anti-pattern (the way done in express). The request object is used as a dumpster for everything in an application, and you can never be sure what’s inside of it. Moreover, it will easily break when you change the order of middleware, with a lot of time for debugging guaranteed.

  2. The second reason is that Koa didn’t have a new version for already a year. Express does a little better, but still lacking. You can call it mature, but the problem is that the libraries have a very outdated core, and don’t support some modern NodeJs features. For example, a new option to manage the maximum requests a socket can handle, introduced in Node.js 16.10.0.

  3. Fastify has built-in support for JSON schema, which is used for request validation with Ajv, fast response serialization with fast-json-stringify, as well as documentation with OpenAPI.

  4. Fastify is fast Benchmarks.

I just don’t see any reason for me to use Express/Koa, the only advantage they have is that they are used in every beginner’s tutorial.

2 Likes

I’ve just came across a good article about middlewares in a telegram channel.
https://insertafter.com/en/blog/no_more_middlewares.html

Ah no you’re absolutely right about the middlewares, I’ve actually been spending some time the last few months refactoring them out of our codebase. Never thought to state it as a flaw of the library, though you’re absolutely right!