Book Image

Beginning Server-Side Application Development with Angular

By : Bram Borggreve
Book Image

Beginning Server-Side Application Development with Angular

By: Bram Borggreve

Overview of this book

Equip yourself with the skills required to create modern, progressive web applications that load quickly and efficiently. This fast-paced guide to server-side Angular leads you through an example application that uses Angular Universal to render application pages on the server, rather than the client. You'll learn how to serve your users views that load instantly, while reaping all the SEO benefits of improved page indexing. With differences of just 200 milliseconds in performance having a measurable impact on your users, it's more important than ever to get server-side right.
Table of Contents (10 chapters)

Creating a Service to Retrieve Data


In this section, we will use ng generate to create the PostsService, use environment to store the API URL, and use the PostsService in our components. We will then define our API calls in the PostsService and leverage the HttpClientModule to enable HTTP access.

Generating the Service

We will use the ng generate service command to generate a service that will handle the interaction with our API:

  1. Open your terminal and navigate to the project directory.

  2. Run the following command from inside the project directory:

    ng g s posts/services/posts --module posts/posts

Storing Our API URL

We will use the environment of Angular CLI to store our API URL. Using the environment, we can define a different URL for development and production environments.

By default, the application generated with Angular CLI comes with two predefined environments. These environments are defined in .angular-cli.json in the project root.

  1. Open the src/environments/environment.ts file.

  2. Inside the environment...