JavaScript - - By Michael Wanyoike

Local REST JSON File

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.

July 27th, 2017: This article series was rewritten with up-to-date information and fresh examples.

In this example, we shall 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": "",
    "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": "",
    "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.

Also: See more JSON examples.