Mocking a Web Service with NPM and JSON Server
This tutroial is part of a series of lessons on Node Package Manager.
In the first lesson in this series of tutorials you downloaded and extracted a folder named npm to serve as a sample project. In this lesson we'll be focusing on one of the files in this folder, it's called sample-data.json
Here's a cool thing you can do with NPM...you can create a mock web service that provides data for your app while you are developing. This is great because building a web service is a big job!
Run this command to install the mock server (it's called json-server)
npm install -g json-server
Note that the -g option tells npm to install the package globally, which means you can use in any project, not just this one.
I've set up a sample data file for the mock server to use, make sure your terminal is in the npm folder (it should have the sample-data.json file in it) and run this command to start the server:
json-server --watch sample-data.json
When the server starts, you should see a URL in the terminal, it will look something like this: http://localhost:3000/posts Go ahead and open a browser and navigate to that URL, you should see the data from the sample file.
To stop the server press Ctrl + C
So this ends our exploration of NPM, there is so much more that you can do than what was covered here. I encourage you to look for new uses and share them with the class