2

I have to build a rest API with Node.JS and TypeScript. I was about to use Express, but It looks like Koa, Fastify and Hapi could provide a better up-to-date experience.

So, what are the modern choices to build a rest api with node and TS in 2021?

Thanks!

maxpaj
  • 6,029
  • 5
  • 35
  • 56
tmos
  • 323
  • 1
  • 3
  • 13

3 Answers3

5

In my opinion, Express.js is the safe choice to go with for building REST APIs with TypeScript in 2021.

Taking a look at the numbers and some other indicators of the popular web frameworks may give you some indication on what is mature and what you'll find community support for. You'll find the following:

Framework TypeScript support GitHub stars GitHub "used by" SO tagged questions npm weekly downloads
Express.js @types/express 51.6k (47.2k) 8.8M (5.5M) 72.7k (59.9k) 15.2M (11M)
Koa.js @types/koa 30.5k (28.4k) 152k (106k) (< 1k) 733k (391k)
Hapi.js @types/hapi__hapi 13k (12k) 15.3k (5.9k) (1.2k) 333k (376k)
Fastify Yes 17k (13.4k) 10.8k (5.3k) (< 1k) 155k (78k)
Loopback 3/4 Yes 13.3k/3.3k 16.1k/3.4k 1.2k 34.8k

Data from January 17, 2021 (within parenthesis are stats from February 4, 2020)


Read more about comparions about specific features:

Resources used:


EDIT: Added data from 2021

maxpaj
  • 6,029
  • 5
  • 35
  • 56
  • Thanks for the answer! A problem I find with looking at the data, is that Express being the oldest and historically a very successfull NPM package, his stats tends to prevent us to see upcoming new solutions. For example, looking at npm trends makes the choice obvious, but at the same time, makes the "express trust" obvious: https://www.npmtrends.com/express-vs-koa-vs-fastify-vs-hapi – tmos Feb 04 '20 at 15:52
  • 1
    I agree. From the data, seems like Fastify is the "up-and-comer" and is perhaps your most promising choice if you are indeed looking for innovation. However, I'd argue that these frameworks rarely change, web frameworks have been "figured out". If you're looking for something more exotic, check out GraphQL. – maxpaj Feb 04 '20 at 16:32
1

I also find this ressource wich looks quite promising and serious about creating rest API: http://restify.com/ it does work with TypeScript, and it looks meant to create exclusively REST apis. Worth a look!

tmos
  • 323
  • 1
  • 3
  • 13
1

I've been using Koa for the past year or so, and I've greatly enjoyed it.

It was actually developed by the same people that developed Express, it just had enough breaking changes from past versions of Express that they decided to give it a new name.

I find the middleware to be less confusing with Koa, and using ctx instead of separate req and res parameters to be much more friendly to read. It comes with a glowing recommendation from me, no complaints so far.

Kevin Hoopes
  • 477
  • 2
  • 8