This tutorial demonstrates how to integrate HTTP and an API into your app.
Up until this point your app has read data from a static array in an Angular service. The next step is to use a JSON server that your app will communicate with over HTTP. The HTTP request will simulate the experience of working with data from a server.
Time required: expect to spend about 20 minutes to complete this lesson.
This lesson starts with the code from the previous lesson, so you can:
If you haven't reviewed the introduction, visit the Introduction to Angular tutorial to make sure you have everything you need to complete this lesson.
Perform these steps in the terminal on your local computer.
JSON Server is an open source tool used to create mock REST APIs. You'll use it to serve the housing location data that is currently stored in the housing service.
Install json-server
from npm by using the following command.
npm install -g json-server
In the root directory of your project, create a file called db.json
. This is where you will store the data for the json-server
.
Open db.json
and copy the following code into the file
{ "locations": [ { "id": 0, "name": "Acme Fresh Start Housing", "city": "Chicago", "state": "IL", "photo": "/assets/bernard-hermant-CLKGGwIBTaY-unsplash.jpg", "availableUnits": 4, "wifi": true, "laundry": true }, { "id": 1, "name": "A113 Transitional Housing", "city": "Santa Monica", "state": "CA", "photo": "/assets/brandon-griggs-wR11KBaB86U-unsplash.jpg", "availableUnits": 0, "wifi": false, "laundry": true }, { "id": 2, "name": "Warm Beds Housing Support", "city": "Juneau", "state": "AK", "photo": "/assets/i-do-nothing-but-love-lAyXdl1-Wmc-unsplash.jpg", "availableUnits": 1, "wifi": false, "laundry": false }, { "id": 3, "name": "Homesteady Housing", "city": "Chicago", "state": "IL", "photo": "/assets/ian-macdonald-W8z6aiwfi1E-unsplash.jpg", "availableUnits": 1, "wifi": true, "laundry": false }, { "id": 4, "name": "Happy Homes Group", "city": "Gary", "state": "IN", "photo": "/assets/krzysztof-hepner-978RAXoXnH4-unsplash.jpg", "availableUnits": 1, "wifi": true, "laundry": false }, { "id": 5, "name": "Hopeful Apartment Group", "city": "Oakland", "state": "CA", "photo": "/assets/r-architecture-JvQ0Q5IkeMM-unsplash.jpg", "availableUnits": 2, "wifi": true, "laundry": true }, { "id": 6, "name": "Seriously Safe Towns", "city": "Oakland", "state": "CA", "photo": "/assets/phil-hearing-IYfp2Ixe9nM-unsplash.jpg", "availableUnits": 5, "wifi": true, "laundry": true }, { "id": 7, "name": "Hopeful Housing Solutions", "city": "Oakland", "state": "CA", "photo": "/assets/r-architecture-GGupkreKwxA-unsplash.jpg", "availableUnits": 2, "wifi": true, "laundry": true }, { "id": 8, "name": "Seriously Safe Towns", "city": "Oakland", "state": "CA", "photo": "/assets/saru-robert-9rP3mxf8qWI-unsplash.jpg", "availableUnits": 10, "wifi": false, "laundry": false }, { "id": 9, "name": "Capital Safe Towns", "city": "Portland", "state": "OR", "photo": "/assets/webaliser-_TPTXZd9mOo-unsplash.jpg", "availableUnits": 6, "wifi": true, "laundry": true } ] }
Save this file.
Time to test your configuration. From the command line, at the root of your project run the following commands.
json-server --watch db.json
In your web browser, navigate to the http://localhost:3000/locations
and confirm that the response includes the data stored in db.json
.
If you have any trouble with your configuration, you can find more details in the official documentation.
The data source has been configured, the next step is to update your web app to connect to it use the data.
In src/app/housing.service.ts
, make the following changes:
Update the code to remove housingLocationList
property and the array containing the data.
Add a string property called and set the value to 'http://localhost:3000/locations'
url = 'http://localhost:3000/locations';
This code will result in errors in the rest of the file because it depends on the housingLocationList
property. We're going to update the service methods next.
Update the getAllHousingLocations
function to make a call to the web server you configured.
async getAllHousingLocations(): Promise<HousingLocation[]> { const data = await fetch(this.url); return await data.json() ?? []; }
The code now uses asynchronous code to make a get
request over HTTP
. Notice, for this example, the code uses fetch. For more advanced use cases consider using HttpClient
provided by Angular.
Update the getHousingLocationsById
function to make a call to the web server you configured.
async getHousingLocationById(id: number): Promise<HousingLocation | undefined> { const data = await fetch(`${this.url}/${id}`); return await data.json() ?? {}; }
Once all the updates are complete, your updated service will match the following code.
import { Injectable } from '@angular/core'; import { HousingLocation } from './housinglocation'; @Injectable({ providedIn: 'root' }) export class HousingService { url = 'http://localhost:3000/locations'; async getAllHousingLocations(): Promise<HousingLocation[]> { const data = await fetch(this.url); return await data.json() ?? []; } async getHousingLocationById(id: number): Promise<HousingLocation | undefined> { const data = await fetch(`${this.url}/${id}`); return await data.json() ?? {}; } submitApplication(firstName: string, lastName: string, email: string) { console.log(firstName, lastName, email); } }
The server is now reading data from the HTTP
request but the components that rely on the service now have errors because they were programmed to use the synchronous version of the service.
In src/app/home/home.component.ts
, update the constructor to use the new asynchronous version of the getAllHousingLocations
method.
constructor() { this.housingService.getAllHousingLocations().then((housingLocationList: HousingLocation[]) => { this.housingLocationList = housingLocationList; this.filteredLocationList = housingLocationList; }); }
In src/app/details/details.component.ts
, update the constructor to use the new asynchronous version of the getHousingLocationById
method.
constructor() { const housingLocationId = parseInt(this.route.snapshot.params['id'], 10); this.housingService.getHousingLocationById(housingLocationId).then(housingLocation => { this.housingLocation = housingLocation; }); }
Save your code.
Open the application in the browser and confirm that it runs without any errors.
In this lesson, you updated your app to:
json-server
)Congratulations! You've successfully completed this tutorial and are ready to continue your journey with building even more complex Angular Apps. If you would like to learn more, please consider completing some of Angular's other developer tutorials and guides.
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/tutorial/first-app/first-app-lesson-14