Lab 17: Setup Backend REST API
Objectives
- Install the backend REST API server
- Create a custom npm script to run the REST API server
- Start the REST API server
Steps
Install the backend REST API server
- Open a command prompt(Windows) orterminal(Mac).
- Change the current directory to working\keeptrack.
- Run one of the following commands:npmnpm install json-server@0.16.2Yarnyarn add json-server@0.16.2
Create a custom npm script to run the REST API server
- Add a script to start the backend REST API.\package.json{"name": "keeptrack",..."scripts": {"start": "react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject",+ "api": "json-server api/db.json --port 4000"},}
- Copy the directory labs\js\snippets\lab17\apiinto theworking\keeptrackdirectory.
Start the REST API server
- In a - command prompt(Windows) or- terminal(Mac) with the current directory set to- working\keeptrack.
- Run one of the following commands: - npmnpm run api- The run command is short for run-script. Running the backend - json-serverthrough an- npm scriptensures that we are using the local version of the server we just installed and not a previously installed global version.- Yarnyarn api
- The server should start and output similar to the following should display. \{^_^}/ hi!Loading api/db.jsonDoneResourceshttp://localhost:4000/projectsHomehttp://localhost:4000Type s + enter at any time to create a snapshot of the database- Note that if you visit - http://localhost:4000/you would normally see the- json-serverlanding page but this will not work when you are inside a- Create React Appproject.
- In your - Chromebrowser open:
- You should see - JSONdata being returned.[{"id": 1,"name": "Johnson - Kutch","description": "Fully-configurable intermediate framework. Ullam occaecati libero laudantium nihil voluptas omnis qui modi qui.","imageUrl": "/assets/placeimg_500_300_arch4.jpg","contractTypeId": 3,"contractSignedOn": "2013-08-04T22:39:41.473Z","budget": 54637,"isActive": false},{"id": 2,"name": "Dillesik LLCs","description": "Re-contextualized dynamic moratorium. Aut nulla soluta numquam qui dolor architecto et facere dolores.","imageUrl": "/assets/placeimg_500_300_arch12.jpg","contractTypeId": 6,"contractSignedOn": "2016-06-26T18:24:01.706Z","budget": 29729,"isActive": true},{"id": 3,"name": "Purdy, Keeling and Smithams","description": "Innovative 6th generation model. Perferendis libero qui iusto et ullam cum sint molestias vel.","imageUrl": "/assets/placeimg_500_300_arch5.jpg","contractTypeId": 4,"contractSignedOn": "2013-05-26T01:10:42.344Z","budget": 45660,"isActive": true},...]