Useful application methods

As mentioned earlier, the application object exposes some methods to make development easier and make some tasks easier to do. In this section, we will discuss some of the methods available in the application method:

  • app.listen(...): The app.listen() method is used to create and return an HTTP Server. It is syntactical sugar around the native Node server.listen() method. It can be used as seen here: 
      const Koa = require('koa');
const app = new Koa();
app.listen(3000);

The preceding code block is essentially the same as the following one:

const http = require('http');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);

One or more Koa applications can be mounted on the same HTTP server, as creating a Koa application does not directly map to starting a server. Here is an example where a single Koa application is started as HTTP and HTTPS on two different ports:

const http = require('http');
const https = require('https');
const Koa = require('koa');
const app = new Koa();
http.createServer(app.callback()).listen(3000);
https.createServer(app.callback()).listen(3001);
  • app.callback(): The app.callback() method returns a callback function to be used by the http.createServer() method for handling a request. Its usage can be seen in the preceding examples, when we start a simple Koa app with the http.createServer() or https.createServer() methods.
  • app.use(function): This is used for registering middleware to the application. It is similar to the app.use() method implemented in Express. It takes the middleware function as its only argument.

A simple middleware for logging the time a request is made can be defined with app.use() as seen here:

app.use(async ctx => {
const currentDateTime = new Date().toLocaleString();
console.log(`${ctx.method} request made to ${ctx.url} at ${currentDateTime}`;
await next();
});
  • app.keys=: This is used to set signed cookie keys. The keys are passed to KeyGrip, and can be set in the following ways:
      app.keys = ['first secret key', 'second secret key']; // passing 
an array of keys

app.keys = new KeyGrip(['first secret key', 'second secret key'],
'sha256'
); // passing KeyGrip instance

Keygrip is a library for signing and verifying data through a rotating credential system in Node.js. With KeyGrip, new server keys can be added and old ones removed regularly, without invalidating the client credentials.

Passing the KeyGrip instance as opposed to passing in an array of keys to app.keys= gives you the flexibility to set other options such as the hmacAlgorithm and encoding for your signed keys. The documentation for KeyGrip and what these options mean can be found here: https://www.npmjs.com/package/keygrip.

The keys may be rotated and are used when signing cookies with the signed: true option:

context.cookies.set('color', 'red', { signed: true });
  • app.context: This is the prototype from which the context object (usually referred to as ctx) is created. It can be used to append more methods to the context object, so as to make them available throughout your application. This can be seen in this example, where a logger is attached:
      app.context.logger = logger();

app.use(async ctx => {
ctx.logger.info('Accessing middleware...')
});

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

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