Table of Contents for
Server Side development with Node.js and Koa.js Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Server Side development with Node.js and Koa.js Quick Start Guide by Olayinka Omole Published by Packt Publishing, 2018
  1. Server Side Development with Node.js and Koa.js Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Server Side Development with Node.js and Koa.js Quick Start Guide
  5. About Packt
  6. Why subscribe?
  7. Packt.com
  8. Contributors
  9. About the author
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the example code files
  18. Download the color images
  19. Code in action
  20. Conventions used
  21. Get in touch
  22. Reviews
  23. Introducing Koa
  24. Technical requirements
  25. What is Koa?
  26. What can you do with Koa?
  27. Why choose Koa?
  28. When you should not use Koa
  29. Koa versus Express
  30. How can this book help you understand Koa better?
  31. Summary
  32. Getting Started with Koa
  33. Technical requirements
  34. Modern JavaScript
  35. A primer on Node
  36. What is async… await?
  37. The promise class
  38. Introducing async
  39. Introducing await
  40. Installing Koa
  41. Using Babel
  42. Starting a server in Koa
  43. Summary
  44. Koa Core Concepts
  45. Technical requirements
  46. The application object
  47. Useful application methods
  48. Settings
  49. The context object
  50. Context object API
  51. Aliases
  52. The request object
  53. Content negotiation
  54. The response object
  55. Middleware
  56. Cascading in Koa
  57. Defining middleware
  58. Registering middleware
  59. Common middleware
  60. Summary
  61. Handling Errors in Koa
  62. Technical requirements
  63. Catching errors in Koa
  64. Koa's default error handler
  65. Emitting errors
  66. Error event listener
  67. Throwing HTTP errors
  68. Writing error handlers
  69. Summary
  70. Building an API in Koa
  71. Technical requirements
  72. Project setup
  73. Initialization
  74. Installing dependencies
  75. Structure
  76. Building the application
  77. Starting the server
  78. Using Nodemon
  79. Connecting to a database
  80. Creating data models
  81. Setting up the router
  82. Setting up a logger
  83. Creating contact endpoints and controller actions
  84. Retrieving all contacts
  85. Storing new contacts
  86. Retrieving a single contact
  87. Updating a contact
  88. Deleting a contact
  89. Validating requests
  90. Useful notes
  91. Summary
  92. Building an Application in Koa
  93. Technical requirements
  94. About the application
  95. Setting up a project
  96. Installing dependencies
  97. Project structure
  98. Building the application
  99. Starting the server
  100. Connecting to the database
  101. Creating data models
  102. The user model
  103. The post model
  104. Setting up the router
  105. Setting up the views
  106. Using partials
  107. Setting up sessions
  108. Handling authentication
  109. User registration and login
  110. Authentication middleware
  111. Creating controller functions
  112. Summary
  113. Other Books You May Enjoy
  114. Leave a review - let other readers know what you think

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 very fast and is a great choice for building HTTP applications. It processes incoming requests in a loop, called the event loop, which allows the development of fast web servers in JavaScript. Its event-driven architecture allows asynchronous operations. This means that developers can create highly scalable applications capable of processing requests asynchronously without using threading.

Asynchronous programming in Node is one of the reasons the language is so widely adopted. If you are unfamiliar with asynchronous programming or its benefits and how it compares to synchronous programming, here is a good example of a program that needs to make a request to get data from two external sources:

  • In a synchronous program: The logical thing to do would be to make a request to the first external source, get a response, and then make another request to the second external source and merge the results. While this is a flow that is logical and easy to follow, it means that the wait time to service another request will be at least the sum of the wait times for each individual request. Since synchronous code leads to resource and event blocking, it does not lead an efficient solution and effectively slows down our application due to poor resource utilization.
  • In an asynchronous program: Both requests can be made in parallel. When each request is completed, it notifies the main program and the results can be combined after the request that took the longest is completed. In this case, the wait time is only the time it takes for the slower request to be completed. Also, neither of the requests cause resource/event blocking, which would allow our program to respond to more new requests while waiting for results for the initial task.

Managing asynchronous actions can get quite complicated, especially in programs where the flow of logic should be synchronous. Callback functions can be used to manage asynchronous operations. Callback functions are functions that are passed to another function (the main function) to be executed inside the main function. Here's a simple example of using a callback function with the setTimeout() function:

function logTimeUp() {
console.log(“Time up!”);
}

setTimeout(logTimeUp, 1000);

The setTimeout function in JavaScript waits a given number of milliseconds and then executes the callback function passed to it. In the previous code example, we define a callback function called logTimeUp that simply prints Time up! to stdout. We then pass this function as a parameter to the setTimeout function, which will execute the callback function after 1000 milliseconds (one second). This is a classic example of how callbacks work.

In modern JavaScript, asynchronous actions can be modeled using Promises, which can be managed and consumed in multiple ways. One of these ways is using the async… await syntax.