16 Jul 2024 By gauri-datey
We will create health-check module which consists health-check route, component, template and service. Health-check response will be retrieved from backend api.
I will be using Visual Studio Code as my IDE. Open Visual Studio IDE, select the project folder, click on open.
You can user internal IDE terminal to execute below commands. By default internal terminal points to project root directory. We will create health-check and all related files under it's own module .
<project root>$ ng g m health-check --routing=true
<project root>$ ng g c health-check
<project root>$ ng g s health-check/health-check
you can see all related files got added under health-check directory.
Give reference of health-check routing, component inside health-check module in case it is not updated automatically. There is no need to give reference for health-check service as providedIn: 'root' inside service removes the need to import the library.
src/app/health-check/health-check.module.ts
@NgModule({
declarations: [HealthCheckComponent],
imports: [CommonModule, HealthCheckRoutingModule]
})
export class HealthCheckModule {}
As per docs httpClient is a mechanism to communicate to remote server over http. So adding httpClient module inside health-check module.
src/app/health-check/health-check.module.ts
import { HttpClientModule } from '@angular/common/http';
...
@NgModule({
...
imports: [
..
HttpClientModule
]
})
Inject httpClient into service constructor and create a method getServerHealthCheck(); which uses httpClient get method to connect to remote API and returns the observable. Here we are assuming our backend API is running on localhost:3000/api. We will create backend api soon.
src/app/health-check/health-check.service.ts
export class HealthCheckService {
constructor(private httpClient: HttpClient) {}
getServerHealthCheck(): Observable<any> {
return this.httpClient.get<any>('http://localhost:3000/api/healthcheck');
}
}
Inside component create a method which calls above service method and also subscribes to get the response from backend server. We expect the response object as a JSON with message as key. Store the response message inside result variable. This method is called during component initialization. Finally result is displayed on health-check template.
src/app/health-check/health-check.component.ts
export class HealthCheckComponent implements OnInit {
result: string;
constructor(private healthCheckService: HealthCheckService) {}
ngOnInit() {
this.getServerHealthCheck();
}
getServerHealthCheck() {
this.healthCheckService
.getServerHealthCheck()
.subscribe(
(response: any) => {
this.result = response.message;
},
() => {}
);
}
}
src/app/health-check/health-check.component.html
Healthcheck Server Result: <b>{{result}}</b>
Add /healthcheck route inside health-check routing module and health-check component as a corresponding component.
src/app/health-check/health-check-routing.module.ts
const routes: Routes = [{ path: 'healthcheck', component: HealthCheckComponent }];
Finally add reference of health-check module inside main app module.
src/app/app.module.ts
@NgModule({
...
imports: [
...
HealthCheckModule
]
...
})
Covered in last article of this chapter.
Copyright: Copyright is as per the parent material.
Document Version:2