Search icon CANCEL
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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781789345391
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 30, 2018
Length: 132 pages
Edition : 1st
Language : English
ISBN-13 : 9781789345391
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%
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
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
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.