Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Server Side development with Node.js and Koa.js Quick Start Guide
Server Side development with Node.js and Koa.js Quick Start Guide

Server Side development with Node.js and Koa.js Quick Start Guide: Build robust and scalable web applications with modern JavaScript techniques

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Server Side development with Node.js and Koa.js Quick Start Guide

Getting Started with Koa

One of the things that makes Koa so desirable to work with is the ease of getting started with it. Taking after Express, all of the logic for a simple web application can be contained in a single JavaScript file with Koa. This works to our benefit and is especially great if you are just willing to test the framework out before taking a deep dive.

In this chapter, we are going to get our hands dirty and get into the code. We will be exploring the basic concepts of Koa and will build our first server-side application in Koa.

Some of the topics we will cover in this chapter include the following:

  • Modern JavaScript
  • A primer on Node
  • What is async… await?
  • Using Babel
  • Installing Koa
  • Starting a server in Koa

Technical requirements

To follow along with this chapter, you need the following installed locally:

  • Node.js (>= v7.6.0) and NPM: You can find download and installation instructions for this on the Node.js official website (https://nodejs.org/en/).

If you use macOS, you can make use of the Homebrew package manager to install Node.js and MongoDB easily.

The code files of this chapter can be found on GitHub:
https://github.com/PacktPublishing/Server-Side-development-with-Node.js-and-Koa.js-Quick-Start-Guide/tree/master/Chapter02

Check out the following video to see the code in action:
http://bit.ly/2Q2gRoV

Modern JavaScript

Before we get into Node.js and writing code in Koa, we should take a quick look at the modern JavaScript ES6, ES7 (and beyond) syntaxes and the advantages they bring. ES6 (or ES 2015) and ES7 (or ES 2016) are major updates to the JavaScript language that brought a lot of new features and optimized the language for readability and simplicity.

Some of the key features introduced by ES6 include let, const, object destructuring, the spread operator, and so on. The async... await syntax, which is heavily used by Koa, is a new feature introduced in ES7. Some other key features introduced by ES7 are destructuring assignment (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) and Observables (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe). Beyond ES6 and Es7, a lot of...

A primer on Node

As JavaScript developers who may or may not have experience working with Node.js, a brief introduction to Node.js and its core ideology will help get everyone up to speed. Node.js, or simply Node, is a run-time environment that executes JavaScript outside a browser. In simpler terms, and as it relates to web developers, Node is a platform that allows developers to write JavaScript applications that can also act as servers.

JavaScript became popular for being a language used to manipulate the DOM (Document Object Model) on web pages. It was a language typically used for client-side scripting. Node, which was built on Chrome's open source v8 JavaScript engine, made it possible to run JavaScript both on the browser and the server. This was highly accepted, as developers could now develop applications with the same language on servers and web browsers.

Node is...

What is async... await?

Asynchronous functions are functions that work asynchronously. They return a Promise class implicitly and can run concurrently. We will be discussing two major ways to define asynchronous functions. These include the following:

  • Using the native Promise class
  • Using the modern Async keyword

The await keyword is used inside an asynchronous function to resolve a promise. Async and await are usually used together for managing the control of flow in a modern asynchronous JavaScript application. Koa relies heavily on async.. await to avoid callback hell and provide a more convenient method for handling errors.

The promise class

Before introducing async, let's discuss a little about the native Promise...

Installing Koa

To install Koa, you need to have the following installed locally:

  • Node (preferably > 7.6): This can be obtained from the Node homepage (https://nodejs.org/en).
  • NPM: This is the official package manager for JavaScript and is usually installed along with the Node installation.

With Node and NPM installed, you only need to run this command on your Terminal to install Koa to your application:

npm install Koa
Note: If you are using an npm of 5 and above, you do not need to use the --save flag to save installs as dependencies in your package.json. If you are using a lesser version, you will need to add the flag. You can also simply run npm i koa to install Koa and save it as a dependency in newer versions of npm.

Using Babel

If you're using an older version of Node (< 7.6), you will need to use a transpiler such as Babel to make async functions compatible with your version of Node. Koa recommends using Babel's require hook, as seen in the example in the following code snippet (https://babeljs.io/docs/en/babel-register/):

require('babel-register');

// require the rest of the app that needs to be transpiled after the hook

const app = require('./app');

You can then install the transforms you would need, depending on your version of Node. If you are using V6 of Node, you would not need most of the transforms, since it already supports a lot of ES6 features. At the minimum though, you would need the transform-async-to-generator plugin. You can define this in your .babelrc file, as follows:

{
"plugins": ["transform-async-to-generator"]
}

According...

Starting a server in Koa

To get started with starting a server in Koa, we should first create a project directory and enter that directory. We can do so with the following commands:

mkdir koa-server
cd koa-server

Next, we initialize a project in npm with the following command:

npm init

After running this command, follow the prompts to help create a package.json file for your project.

You can run npm init –y to create a package.json file for your project with default values.

Next, we can install Koa to our project with the following command:

npm i koa

Now that we have Koa installed, we can create our server file. Let's call the file index.js. If you are using a Unix-based operating system such as Linux or macOS, you can create the file with the following command:

touch index.js

This file will serve as the entry point to our application, and we will write the main...

Summary

In this chapter, we learned about Node and how asynchrony in JavaScript works. We also learned about the different ways asynchronous actions in JavaScript are handled, including the async… await syntax.

After the primer, we dug into writing some code and started a very light server in Koa. This showed us the prerequisites of Koa, how to install the framework, and how to get started on it.

In the next chapter, we will learn about the core concepts of Koa. We will learn about the application, context, request, and response objects in Koa. We will also learn how to create and use middleware. We will go into details about Koa's philosophy and show readers how the different parts of the framework work.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Get up and running with Koa.js and leverage its power with node.js
  • Get the most out of Koa Async functions and generators
  • Create real time dynamic serverside apps efficiently with Koa.js

Description

Every developer wants to build modular and scalable web applications. Modern versions of JavaScript have made this possible in Node.js, and Koa is a Node.js framework that makes it easy. This book is the ideal introduction for JavaScript developers who want to create scalable server side applications using Node.js and Koa.js. The book shows you how Koa can be used to start projects from scratch, register custom and existing middleware, read requests, and send responses to users. We will explore the core concepts in Koa, such as error handling, logging, and request and response handling. We will dive into new concepts in JavaScript development, and see how paradigms such as async/await help with modern Node.js application development. By the end of this book, you will be building robust web applications in Koa using modern development paradigms and techniques of Node.js development.

Who is this book for?

This book is for serverside developers and JavaScript developers who want to use Koa.js and Node.js to create fast and real time back end applications.

What you will learn

  • Create a simple server in Node.js and Koa
  • Work with custom middleware in Koa
  • Handle errors in Koa
  • Create routes, handle requests, and send responses from APIs
  • Build views and use templates in Koa
  • Authenticate your application and structure it properly in Koa

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 30, 2018
Length: 132 pages
Edition : 1st
Language : English
ISBN-13 : 9781789343663
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Nov 30, 2018
Length: 132 pages
Edition : 1st
Language : English
ISBN-13 : 9781789343663
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 98.97
Node.js Web Development
€36.99
Modern JavaScript Web Development Cookbook
€36.99
Server Side development with Node.js and Koa.js Quick Start Guide
€24.99
Total 98.97 Stars icon

Table of Contents

7 Chapters
Introducing Koa Chevron down icon Chevron up icon
Getting Started with Koa Chevron down icon Chevron up icon
Koa Core Concepts Chevron down icon Chevron up icon
Handling Errors in Koa Chevron down icon Chevron up icon
Building an API in Koa Chevron down icon Chevron up icon
Building an Application in Koa Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(2 Ratings)
5 star 50%
4 star 50%
3 star 0%
2 star 0%
1 star 0%
Greg Tippett Oct 17, 2021
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is a great intro to Koa for readers already familiar with Node. The example app is well-organized and the reader is left with a good basic reference.Improvements for a second edition could include discussion of various concepts such as the differences between session and state, the use of session secret keys, and why the method-override middleware is needed.
Amazon Verified review Amazon
Zach James Jan 27, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a good book that is updated for Koa2. Enjoyable to read!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.