1.0.3 • Published 2 years ago

bbe-json-server v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Introduction

It's a javascript Based Json API, it create json files for your data and it uses node http server to create a Json server for your UI testing.

Installation

npm i bbe-json-server

Usage

Node

// This
var bjs = require('bbe-json-server');
// Or
var { run } = require('bbe-json-server');

var PORT = 3000

// This
bjs.run(PORT,()=>{
    console.log(`server runing on port: ${PORT}`)
});
// Or 
run(PORT,()=>{
    console.log(`server runing on port: ${PORT}`)
});

TypeScript

// This 
import * as bjs from 'bbe-json-server';
// Or
import { run } from 'bbe-json-server';

var PORT = 3000

// This 
bjs.run(PORT,()=>{
    console.log(`server runing on port: ${PORT}`)
});
// Or
run(PORT,()=>{
    console.log(`server runing on port: ${PORT}`)
});

Http Requests

import axios from 'axios';


// Post a Doc to Posts
axios.post('http:/localhost:3000/posts',{
    content: 'my first post',
    author: 'Fred'
  })
  .then(function (response) {
    // handle success
    console.log(response);
    // { data: { id: 1, content: 'my first post', author: 'Fred'}}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });


// Post a Docs to Posts
axios.post('http:/localhost:3000/posts',[
    {
    content: 'my first post',
    author: 'Fred'
    },{
    content: 'my second post',
    author: 'Fred'
    }
  ])
  .then(function (response) {
    // handle success
    console.log(response);
    // { data: [{ id: 1, content: 'my first post', author: 'Fred'},{ id: 2, content: 'my second post', author: 'Fred'}]}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });


// Update a Doc on Posts
axios.put('http:/localhost:3000/posts/1',{
    author: 'Mike'
    })
  .then(function (response) {
    // handle success
    console.log(response);
    // { data: { id: 1, content: 'my first post', author: 'Mike'}}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });


// Delete a Doc from Posts
axios.delete('http:/localhost:3000/posts/1')
  .then(function (response) {
    // handle success
    console.log(response);
    // { msg: 'doc has been removed'}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });

// Get All Docs on Posts
axios.get('http:/localhost:3000/posts')
  .then(function (response) {
    // handle success
    console.log(response);
    // { data: [...docs],total: `docs count`}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });

// Get Docs on Posts With pagination
axios.get('http:/localhost:3000/posts?page=1&limit=15')
  .then(function (response) {
    // handle success
    console.log(response);
    // { data: [...docs],total: `docs count`}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });

// Get one Doc from Posts
axios.get('http:/localhost:3000/posts/2')
  .then(function (response) {
    // handle success
    console.log(response);
    // { data: {...Doc}}
  })
  .catch(function (error) {
    // handle error
    console.log(error);
    // { message: `Error message`}
  });