Chapter 12. Interacting with servers using HTTP

 

This chapter covers

  • Working with the HttpClient service
  • Creating a simple web server using the Node and Express frameworks
  • Developing an Angular client that communicates with the Node server
  • Intercepting HTTP requests and responses

Angular applications can communicate with any web server supporting HTTP, regardless of what server-side platform is used. In this chapter, we’ll show you how to use the HttpClient service offered by Angular. You’ll see how to make HTTP GET and POST methods with HttpClient. And you’ll learn how to intercept HTTP requests to implement cross-cutting concerns, such as global error handling.

This chapter starts with a brief overview of Angular’s HttpClient service, and then you’ll create a web server using the Node.js and Express.js frameworks. The server will serve the data required for most code samples in this chapter.

Finally, you’ll see how to implement HTTP interceptors and report progress while transferring large assets.

12.1. Overview of the HttpClient service

Browser-based web apps run HTTP requests asynchronously, so the UI remains responsive, and the user can continue working with the application while HTTP requests are being processed by the server. Asynchronous HTTP requests can be implemented using callbacks, promises, or observables. Although promises allow you to move away from callback hell (see section A.12.2 in appendix A), they have the following shortcomings:

12.2. Reading a JSON file with HttpClient

12.3. Creating a web server with Node, Express, and TypeScript

12.4. Bringing Angular and Node together

12.5. Posting data to the server

12.6. HTTP interceptors

12.7. Progress events

Summary

sitemap