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

The response object

The Koa response object is an abstraction of Node's response object. Like the request object, it provides added functionality with its properties and methods for building out everyday HTTP servers.

The methods and properties it exposes include the following:

  • response.header: This returns the response header object.
  • response.headers: This returns the response header object. It is an alias of response.header.
  • response.socket: This returns the request socket.
  • response.status: This returns the response status code, which is 404 by default. This is in contrast to Vanilla Node, where the default status for res.statusCode is 200.
  • response.status=: This is used to set the response status code to a valid HTTP numeric status code.
  • response.message: This returns the response status message. By default, this is associated with the response.status:
      ctx.response.status = 202;
console.log(ctx.response.message);
// => Accepted
  • response.message=: This is used to set the response status message to the supplied value.
  • response.length=: This is used to set the response Content-Length header value to the given value.
  • response.length: This returns the Content-Length as a number when available. It can also return the length by evaluating the content from the ctx.body when possible. It returns undefined when both the Content-Length and ctx.body are unavailable.
  • response.body: This returns the response body. Popularly aliased as ctx.body.
  • response.body=: This sets the response body to one of the following:
    • String
    • Buffer
    • Stream
    • Object/Array
    • Null
  • response.get(field): This returns the value of the field header. The field comparison is case-insensitive, as shown:
      ctx.body = 'Hello World';
console.log(ctx.response.get('content-length'));
// => 11
  • response.set(field, value): This is used to set the response header field to a defined value as shown here:
      ctx.set('Content-Language', 'en');
  • response.append(field, value): This appends an additional header field with a value.
  • response.set(fields): This sets several response header fields as an object as shown:
      ctx.set({
'Content-Language': 'en',
'Retry-After': 120
});
  • response.remove(field): This removes a header field.
  • response.type: This returns the response Content-Type.
  • response.type=: This sets the Content-Type via mime string or file extension.
  • response.is(types...): This checks whether the response type is one of the types specified.
  • response.redirect(url, [alt]): This performs a 302 permanent redirect to a specified url. The string back provides referrer support. When the referrer is not present, alt or / is used.

To alter the default 302 status, assign the status before or after the redirect call. To alter the response body, assign it after the call:

ctx.response.status = 301;
ctx.redirect('back');
ctx.body = "redirecting you to the previous page...";

  • response.attachment([filename]): This sets the Content-Disposition to attachment and optionally specifies the filename. Setting the Content-Disposition to attachment readies the client to receive the response as a download.
  • response.headerSent: This returns a Boolean specifying if a response header has already been sent.
  • response.lastModified: This returns the Last-Modified header as a Date if it exists.
  • response.lastModified=: This sets the last-modified to a UTC string. This method can either be supplied a Date object or a simple date string.
  • response.etag=: This sets the ETag header to a specified value.
  • response.vary(field): This is used to set the value of the Vary header to a specified field.
  • response.flushHeaders(): This method flushes any set headers and begins the response body.