The application object

The application object in Koa is an object containing the Koa application instance. It also contains a list of the middleware functions in the application. It is responsible for managing and executing the middleware in a cascaded manner. It is also responsible for managing some key aspects of the application, as we will soon see in some of the following examples. Here is an excerpt from the Koa official documentation:

"A Koa application is an object containing an array of middleware functions which are composed and executed in a stack-like manner upon request. Koa is similar to many other middleware systems that you may have encountered such as Ruby's Rack, Connect, and so on—however, a key design decision was made to provide high level sugar at the otherwise low-level middleware layer. This improves interoperability, robustness, and makes writing middleware much more enjoyable."

The application object also exposes methods for common tasks such as content-negotiation, cache freshness, proxy support, and so on. A useless Koa application that runs on port 1234 can be easily created in just a few lines of code, as shown here:

const Koa = require('koa');
const app = new Koa();

const port = 1234;
app.listen(port, () => {
console.log(`The app is running on port ${port}`);
});

In the preceding application, the application object is referenced by the app variable by instantiating the application with new Koa(). We then use the application object to start the server with app.listen(), one of the various methods exposed on the application object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.70.203