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
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. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/housing.service.ts
The code now uses asynchronous code to make a GET request over HTTP.
HELPFUL: 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. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/housing.service.ts
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. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/home/home.component.ts
In src/app/details/details.component.ts, update the constructor to use the new asynchronous version of the getHousingLocationById method. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/details/details.component.ts
1. Configure the JSON server
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.
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
Save this file.
Time to test your configuration. From the command line, at the root of your project run the following commands.
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.
2. Update service to use web server instead of local array
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 url and set its value to '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. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/housing.service.ts
The code now uses asynchronous code to make a GET request over HTTP.
HELPFUL: 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. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/housing.service.ts
Once all the updates are complete, your updated service should match the following code. Final version of housing.service.ts
3. Update the components to use asynchronous calls to the housing service
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. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/home/home.component.ts
In src/app/details/details.component.ts, update the constructor to use the new asynchronous version of the getHousingLocationById method. adev/src/content/tutorials/first-app/steps/14-http/src-final/app/details/details.component.ts
Save your code.
Open the application in the browser and confirm that it runs without any errors.