Server-Specific Service

Once your MongoService is in place, you need to prepare a server-equivalent of ProductsService. This service will query MongoService for products list and descriptions and embed those in the HTML rendered by Angular Universal.

Providing Angular With Server-Specific Service Implementation#

It's time to consume MongoService. You could do this using the isPlatformServer() and isPlatformBrowowser() methods that you already know. The problem with this solution is that server-specific code is included in the main bundle that is shipped to the browser. In other words, this solution increases the size of the application delivered to the end user.

Fortunately, another technique is available. You are going to make use of AppServerModule and provide the implementation of the ProductsService interface that is specific to the server runtime.

Implementing the server-specific service#

Generate a new service using the following command:

Copy the following service logic to src/app/products-server.service.ts:


This page is a preview of The newline Guide to Angular Universal

Start a new discussion. All notification go to the author.