/json-server-with-data

Quick starter for JSON API server with existing data

Primary LanguageJavaScript

JSON API server with existing data

Status GitHub Issues GitHub Pull Requests


Quick starter for JSON API server with existing data

📝 Table of Contents

About

Quickly get up and running with JSON Server with existing data - built with json-server and FakerJS

Getting Started

Clone project and install depenecies

npm i

And start

npm run start

Using the endpoints

Available endpoints

  • users (simple info + avatar + array of Posts)
  • posts (simple info + userId if created from User)
  • images (simple info + galleryId if created from Gallery)
  • galleries (simple info + array of Images)
  • employees (lot of info + companyId if created from Company)
  • companies (simple info + image + array of Employees)

All HTTP endpoints can be used with REST actions:

GET    /users
GET    /users/{id}
POST   /users
PUT    /users/{id}
PATCH  /users/{id}
DELETE /users/{id}

Using queries

http://localhost:3000/users?firstname=John

GET examples

GET   http://localhost:3000/users
GET   http://localhost:3000/posts
GET   http://localhost:3000/images
GET   http://localhost:3000/galleries
GET   http://localhost:3000/employees
GET   http://localhost:3000/companies

Modify data

For seperations of concerns each REST API endpoints is located in data/ as a .js file:

data/users.js

var faker = require('faker');
var generatePosts = require('./posts');

module.exports = function (amount) {
  var entities = [];

  for (var id = 1; id <= amount; id++) {
    let entity = {
      id: id,
      firstname: faker.name.firstName(),
      lastname: faker.name.lastName(),
      username: faker.internet.userName(),
      email: faker.internet.email(),
      password: faker.internet.password(),
      color: faker.commerce.color(),
      avatar: faker.image.avatar(),
      Posts: generatePosts(faker.random.number(10), id),
    };
    entities.push(entity);
  }

  return entities;
};

Control the endpoints to include, and the amount of entities to be created:

db.js

function generateDB() {
  return {
    employees: require('./data/employees')(50),
    users: require('./data/users')(50),
    posts: require('./data/posts')(50),
    images: require('./data/images')(20),
    galleries: require('./data/galleries')(5),
    companies: require('./data/companies')(3),
  };
}
module.exports = generateDB;