Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 984 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Create a modern web API using Koa v2 and ES2017

1:25 Node.js lesson by

We take advantage of the new Node.js features like async/await from ES2017 and the awesome yarn package-manager to create modern JSON APIs with Koa v2. Make sure you have at least Node.js 7.6.x installed.

The ctx (context) variable encapsulates a Request and Response object, they are similar to what we already know about the req and res objects from the Express.js framework, but it is more expressive and easy to understand thanks to its getters, _setters and some shortcuts. It also parses the response content and sets the Content-Type header based on the type of its body property. Finally, we can manually set the response headers and status code.

The HTTP client from the video is wuzz.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We take advantage of the new Node.js features like async/await from ES2017 and the awesome yarn package-manager to create modern JSON APIs with Koa v2. Make sure you have at least Node.js 7.6.x installed.

The ctx (context) variable encapsulates a Request and Response object, they are similar to what we already know about the req and res objects from the Express.js framework, but it is more expressive and easy to understand thanks to its getters, _setters and some shortcuts. It also parses the response content and sets the Content-Type header based on the type of its body property. Finally, we can manually set the response headers and status code.

The HTTP client from the video is wuzz.

We start creating a new node.js application, and we install Koa. With [inaudible 0:04] , this is pretty fast. Then, we go to our JavaScript file, and we enable strict mode. We import Koa class constructor from the install module using the required statement.

Now, we can create an app instance from the imported class. The elegance of Koa -- it's all about middlewares. A middleware is just a function which, first argument is a context variable. Here, we can set the body response content to a JavaScript object.

To start our application, we have to listen to a specific port. We will go to our package, that JSON file, and we'll create a script session. The start command will run node on our index.js file. Going back to the terminal, we run [inaudible 0:54] start. Then, using any HTTP client, we perform a git request.

We should expect a JSON response with its respective header. Furthermore, we have control over the context variable. Here, in this example, we will simulate a not found response. To try this, we must restart our application. Finally, we can see that the status code has changed.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?