Local REST JSON File

Share this article

This article series was rewritten in mid 2017 with up-to-date information and fresh examples.

In modern web development it is common to set up API services to connect applications to databases. Often, API services will use XML or JSON formats to exchange data between the client and the server.

In this example, we’ll look at a custom NodeJS API service built using FeathersJS. If you haven’t done so, download the project as documented in the Intro JSON Examples page.

Next, ensure you have MongoDB running on your system. By default, the API service will access a database called api and will create or overwrite a collection named customers. You can set up your own database connection parameters in the following file api/config/default.json if you need to change it:

// api/config/default.json`

{
  "host": "localhost",
  "port": 3030,
  "public": "../public/",
  "paginate": {
    "default": 10,
    "max": 50
  },
  "mongodb": "mongodb://localhost:27017/api"
}

Next, install dependencies and start the API server:

cd api
npm install
npm start

After a few seconds, some fake data will be generated and then the API service will be ready to serve requests. Launch your browser with the URL: http://localhost:3030/customers. You should be greeted by generated customer data in JSON format. Below is the output in pretty format:

Partial Output:

{
  "total": 25,
  "limit": 10,
  "skip": 0,
  "data": [{
    "_id": "5968fcad629fa84ab65a5247",
    "first_name": "Sabrina",
    "last_name": "Mayert",
    "address": "69756 Wendy Junction",
    "phone": "1-406-866-3476 x478",
    "email": "donny54@yahoo.com",
    "updatedAt": "2017-07-14T17:17:33.010Z",
    "createdAt": "2017-07-14T17:17:33.010Z",
    "__v": 0
  }, {
    "_id": "5968fcad629fa84ab65a5246",
    "first_name": "Taryn",
    "last_name": "Dietrich",
    "address": "42080 Federico Greens",
    "phone": "(197) 679-7020 x98462",
    "email": "betty_schaefer1@gmail.com",
    "updatedAt": "2017-07-14T17:17:33.006Z",
    "createdAt": "2017-07-14T17:17:33.006Z",
    "__v": 0
  },
  ...
  ]
}

You can then use this JSON data to populate your front-end views. The beauty of JSON is that it abstracts the underlying technology running your database. You can easily switch to a different database type without changing your front-end logic.

Here are the other examples in this series:

Frequently Asked Questions about Local REST JSON File

How can I create a local REST API for testing purposes?

Creating a local REST API for testing purposes involves several steps. First, you need to create a JSON file that will serve as your database. This file should contain the data you want to test with. Next, you need to install json-server, a tool that allows you to run a fake REST API with zero coding. Once installed, you can start the server by running the command “json-server –watch db.json”. This will start the server and watch your database file for any changes. You can then use tools like Postman or curl to send requests to your API and see the responses.

What is the purpose of a local API server?

A local API server is primarily used for development and testing purposes. It allows developers to simulate the behavior of a real API without having to connect to a live server. This can be particularly useful when developing applications that rely on API data, as it allows for testing and debugging in a controlled environment. It can also be used for learning and experimenting with APIs without the risk of affecting live data.

How can I use Payload CMS for local API development?

Payload CMS is a powerful tool for building APIs. To use it for local API development, you first need to install it and set up a new project. Once your project is set up, you can define your collections and fields in the Payload config file. Payload will then automatically generate a RESTful API based on your configuration. You can use this API for local development and testing, and when you’re ready, you can deploy it to a live server.

What is Stackery and how can it help with local API development?

Stackery is a tool for managing serverless applications. It provides a visual editor for designing your application architecture, and it automates many of the tasks involved in deploying and managing serverless applications. For local API development, Stackery provides a local development environment that allows you to run and test your serverless applications locally. This can be a great help in speeding up the development process and catching bugs early.

Are there any video tutorials that can help me learn how to create a local REST API?

Yes, there are many video tutorials available online that can help you learn how to create a local REST API. For example, the video tutorial at https://www.youtube.com/watch?v=V7sLq7u28BA provides a step-by-step guide on how to create a local REST API using json-server. These tutorials can be a great resource for visual learners, and they often provide practical examples that can help you understand the concepts better.

Michael WanyoikeMichael Wanyoike
View Author

I write clean, readable and modular code. I love learning new technologies that bring efficiencies and increased productivity to my workflow.

jsonnilsonjREST
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week