koa_2.png

With the popularity of ES6, the async/await syntax is favored by more JS developers. Koa. JS, as a relatively early Node framework that supports the use of this syntax, is becoming more and more popular. This article has selected ten of them that will be particularly useful when developing applications or frameworks.

No.1 koa-router

Routing is an essential basic function of Web framework. In order to keep itself simple, KOA. js does not have routing function like Express.js. Therefore, KOA-Router is a good supplement. For example, Express app.get/ POST/PUT writing, URL naming parameters, route naming, support for loading multiple middleware, nested routes, etc. Other optional routing middleware: KOA-route, KoA-Joi-Router, and Koa-trie-Router

No.2 koa-bodyparser

Koa.js does not have a built-in Request Body parser, so we need to load additional middleware when parsing the Request Body. The official koA-BodyParser is a good choice. We support X-www-form-urlencoded, Application/JSON request bodies, but do not support form-data request bodies, and we need to use the formidable library. You can also use koA-body or koA-better-body directly

No.3 koa-views

Koa-views is an indispensable middleware for applications that require view template rendering, supporting ejS, Nunjucks and many other template engines.

No.4 koa-static

In addition to handling dynamic requests, Node.js can also be used as a nginx-like static file service, which is particularly convenient for local development. It can be used to load front-end files or back-end Fake data, and can be used in combination with KOA-COMPRESS and KOa-mount.

No.5 koa-session

HTTP is a stateless protocol. In order to maintain user status, we generally use Session sessions. Koa-session provides such functions as storing Session information in local cookies and external storage devices such as Redis and MongoDB.

No.6 koa-jwt

More and more websites are switching from Session Base to Token Base. JWT(Json Web Tokens) is an open standard adopted by many websites. Koa-jwt middleware uses JWT to authenticate HTTP requests.

No.7 koa-helmet

Web Security is getting more and more attention. Helmet improves the Security of Express applications by adding HTTP headers such as strict-transport-Security, X-frame-options, x-frame-options, etc. Koa-helmet provides similar functionality for KOA applications, see the Node.js security checklist.

No.8 koa-compress

When the response volume is large, a compression technology such as Gzip is usually enabled to reduce the transmission content. Koa-compress provides such a function, which can be flexibly configured as required.

No.9 koa-logger

The KOA-Bunyan-Logger outputs request logs, including the REQUEST URL, status code, response time, and response body size. It is especially helpful for debugging and tracing applications. The KOA-Bunyan-Logger provides more diversified functions.

No.10 koa-convert

For older KOA middleware that uses Generate (< KOA2), a flexible tool is available to convert them to Promise based middleware for KOA2, as well as to convert newer Promise based middleware to older Generate middleware.