JavaScript: json-server

When developing code that calls on APIs (Application Programming Interface) it can be a pain having to call the API multiple times just whilst coding the initial call or during testing.

NPM (A package manager for Node) has a package that solves this called json-server.

It can be installed via:

npm i -g json-server

The i NPM to install and the -g sets it as global install.

json-server can then be run using:

json-server --watch filename.json -p 8000

The –watch switch is optional but I use it to point to a specific JSON (JavaScript Object Notation) file which I want to serve.

The -p switch is also optional and can be used to specify which port json-server should run on. I use it to stop it clashing with ports already in use by other running applications.

json-server then serves the JSON file as a response, and if the file has been populated in a similar way to the real API would respond then it allows for lots of testing / calls without having to call the real API.

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect