Category: 02. Koa Js

  • Error Handling

    Error handling plays an important part in building web applications. Koa uses middleware for this purpose as well. In Koa, you add a middleware that does try { yield next } as one of the first middleware. If we encounter any error downstream, we return to the associated catch clause and handle the error here. For example…

  • Redirects

    Redirection is very important when creating websites. If a malformed URL is requested or there are some errors on your server, you should redirect them to the respective error pages. Redirects can also be used to keep people out of restricted areas of your website. Let us create an error page and redirect to that…

  • Response Object

    A Koa Response object is an abstraction on top of node’s vanilla response object, providing additional functionality that is useful for everyday HTTP server development. The Koa response object is embedded in the context object, this. Let’s log out the response object whenever we get a request. When you run this code and navigate to https://localhost:3000/hello then you’ll…

  • Request Object

    A Koa Request object is an abstraction on top of node’s vanilla request object, providing additional functionality that is useful for everyday HTTP server development. The Koa request object is embedded in the context object, this. Let’s log out the request object whenever we get a request. When you run this code and navigate to https://localhost:3000/hello, then…

  •  HTTP Methods

    The HTTP method is supplied in the request and specifies the operation that the client has requested. The following table summarizes the commonly used HTTP methods. Sr.No. Method & Description 1 GETThe GET method requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect. 2…

  • URL Building

    We can now define routes; they are either static or fixed. To use dynamic routes, we need to provide different types of routes. Using dynamic routes allow us to pass parameters and process based on them. Following is an example of a dynamic route. To test this go to https://localhost:3000/123. You will get the following response.…

  • Routing

    Web frameworks provide resources such as HTML pages, scripts, images, etc. at different routes. Koa does not support routes in the core module. We need to use the Koa-router module to easily create routes in Koa. Install this module using the following command. Now that we have Koa-router installed, let’s look at a simple GET…

  • Generators

    One of the most exciting new features of JavaScript ES6 is a new breed of function, called a generator. Before generators, the whole script was used to usually execute in a top to bottom order, without an easy way to stop code execution and resuming with the same stack later. Generators are functions which can…

  • Hello World

    Once we have set up the development, it is time to start developing our first app using Koa. Create a new file called app.js and type the following in it. Save the file, go to your terminal and type. This will start the server. To test this app, open your browser and go to https://localhost:3000 and you should receive…

  • Environment

    To get started with developing using the Koa framework, you need to have Node and npm (node package manager) installed. If you don’t already have these, head over to Node setup to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal. You should receive an…