gitmyhub

polka

JavaScript ★ 5.6k updated 1y ago

A micro web server so fast, it'll make you dance! :dancers:

Polka is a small, fast web server library for Node.js. It works similarly to Express.js, a very popular library for building web servers in JavaScript, but it is much smaller in terms of code size and benchmarks around 33 to 50 percent faster for simple applications. If you already know how to use Express, you can switch to Polka with very few changes, since the API is nearly identical.

At its core, Polka takes incoming web requests, matches them to routes you define (for example, GET /users/:id), runs any middleware functions you have set up, and sends back a response. Middleware are small functions that run before your main route handler. They can read or modify the request, like checking an authentication token or logging the request details, and then pass control along to the next step. Polka supports standard Express middleware as well as its own.

You can attach Polka to an existing HTTP server you created elsewhere in your application, or let it create one for you when you call the listen method. Sub-applications are also supported, meaning you can build separate Polka instances for different parts of your app (like a /users section or an /admin section) and mount them together.

The README is detailed and covers the full API: how to create a Polka instance, define routes, attach middleware, handle errors, and parse incoming URLs. It also includes a benchmarks section comparing Polka's performance against Express and several other web server libraries, and a section explaining the differences between Polka and Express for developers making the switch.

The library is intended for developers building HTTP services in Node.js who want something lighter and faster than Express while keeping a familiar interface.