Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Server Side development with Node.js and Koa.js Quick Start Guide

You're reading from   Server Side development with Node.js and Koa.js Quick Start Guide Build robust and scalable web applications with modern JavaScript techniques

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789345391
Length 132 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Olayinka Omole Olayinka Omole
Author Profile Icon Olayinka Omole
Olayinka Omole
Arrow right icon
View More author details
Toc

Koa versus Express

Koa and Express share a lot of similarities, as the development of the two frameworks was kickstarted by the same team. While a lot of the underlying philosophies between Koa and Express are the same, clear differences exist as the creators of the frameworks attempted to do things in a different way with the release of Koa.

A major difference is in the philosophy of the two frameworks. Whereas Express complements Node, Koa attempts to fix and replace many things in it. The major difference is the fact that Koa tries to completely ditch callbacks and avoid callback hell by making use of promises and async functions.

Unlike Express, which augments Node's request (req) and response (res) objects with additional parameters and methods, Koa provides its own ctx.request and ctx.response objects. According to the Koa documentation, the following is true:

"Koa can be viewed as an abstraction of Node.js's http modules, whereas Express is an application framework for Node.js."

Koa tries to fix some of the things wrong with Node and provides a simple, lightweight, and unopinionated framework for building out HTTP services.

Some other differences between Koa and Express include the following:

  • Router: Koa does not include a router out of the box; instead, external middleware is available to be used as routers such as koa-router and koa-route. Express, on the other hand, comes bundled with a router out of the box.
  • Templating: Express has support for various popular templating engines out of the box, including Jade, Pug, EJS,and Mustache. In contrast, Koa requires installing an external plugin/middleware to support templating engines. A popular plugin for templating in Koa is koa-views.
  • Convenience utilities: Express includes some convenience utilities (https://expressjs.com/en/resources/utils.html) to help programmers handle regular tasks such as file streaming and URL parsing. Koa does not include these utilities.
  • Promise-based control flow: Koa has the advantage of ditching callbacks and avoiding callback hell by making heavy use of promises, unlike its predecessor, Express. This ensures that errors are easier to handle without many try... catch statements.
  • Cascading middleware pattern of flow in Koa.js: This allows middleware to take action exactly twice for each request whereas express middleware allows for only single execution per request. This flexibility allows Koa middleware developers to use the patterns established in other languages and systems such as Ruby's Rack.

Here is a table from the Koa documentation, comparing it to Express:

Feature Koa Express
Middleware Kernel
Routing
Templating
Sending files
JSONP

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime