Node Cookbook by David Mark Clements
Author:David Mark Clements
Language: eng
Format: epub, pdf
Tags: COM060160 - COMPUTERS / Web / Web Programming, COM051260 - COMPUTERS / Programming Languages / JavaScript, COM021050 - COMPUTERS / Databases / Servers
Publisher: Packt Publishing
Published: 2017-11-29T09:48:27+00:00
How it works...
Since Koa is a bare bones web framework, we installed koa-static and koa-router along with koa and use them in our index.js file.
Essential Knowledge
To understand how Koa works at a basic level, we need a fundamental understanding of JavaScript promises (in particular ES 2015 promises) and of async/await syntax. See https://medium.com/@bluepnume/eb148164a9c8 for an excellent article on how these abstractions interact.
The koa-static module returns a Koa middleware function, which is passed into app.use. Koa middleware accepts a context object (often called ctx) and a next function. The next function always returns a promise.
The koa-static middleware attempts to locate files as per our defined path (the public folder), then creates a write stream from a file (if found) to the ctx.request object, which is Node's core http request object (an instance of http.IncomingMessage often called req). If the file isn't found it passes on control to the next piece of middleware.
The koa-router middleware is superficially similar to the Router utility in Express. However, we register other router instances with our main router instance (our router object in index.js) by calling router.use. This allows us to set mount points for a particular set of routes. Then we pass the main router instance into app.use.
In routes/index.js we load koa-router and call it as a function (the same as we do in index.js) to create another router instance. We can call methods on the router object that correspond to HTTP verbs (such as get, post, delete, put, and so on).
We register the / route with router.get, meaning we've set up a GET route that will respond to requests to the / path.
We've taken a contrived approach to handling this route, in order to demonstrate how control flow works in Koa.
We supply two functions to the router.get call, both of them prefixed with the async keyword. An async function always returns a promise, which Koa is expecting.
Our first function immediately calls the next function with the await keyword (await next()). This means the execution of the function pauses until the promise returned from next is resolved.
The next function will (indirectly) call whichever piece of middleware is next in the stack. In our case, it's the route-specific middleware, that is, the third argument passed to router.get, which is also an async function.
This second function simply sets ctx.state to an object containing a title property. Since it's an async function it returns a promise, which our first function waits to be resolved because await next() in this case relates to the resolution of the next route middleware (the third function supplied to router.get).
The line following await next() then assigns the title constant based on the contents of ctx.state. In this case title will now equal 'Koa'. Then we set ctx.body to our HTML content with the title constant interpolated.
This asynchronous dance is completely unnecessary, we could have just set the title constant directly in the first function and not bothered with the second function. However, the point of supplying this example in this recipe was to showcase Koa's control flow behavior in action.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9916)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9779)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8302)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7782)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7697)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6863)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6595)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6464)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6419)
Kotlin in Action by Dmitry Jemerov(5066)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4318)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(4017)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3808)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3750)
The Ultimate iOS Interview Playbook by Avi Tsadok(3726)
