npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-dummy-api

v3.1.0

Published

NPM package to create dummy JSON API with fake response backed by express server.

Downloads

70

Readme

JSON Dummy API

Introduction

This is simple express backed REST api server which send dummy data as respose as per set by the user. You use this in your front-end project as testing api server.

As this JSON-Dummy-API uses express app at backend you need to constantly detect changes and restart the server that's the where dev. dependancy called nodemon come handy

You can directly use node to start the server but each time you make changes in server configurations or any server related file you have to terminate the server and run node command again and again thats why we are using nodemon for constant change detection and updation

To use nodemon in your project, lets first install it

npm i nodemon --save-dev

you can install globally if you want you just need to add -g flag to the command after you install nodemon next step is to create server.js file

Create the Server config file


Below given is the very basic syntax of the configuration and server starter file you need

const { app , PARAGRAPH } = require("json-dummy-api");

const routes = [
  {
    routeName: "/simpleParagraph",
    response: {
      para: PARAGRAPH,
    },
  },
];

app.startDummyServer(routes);
  • startDummyServer is the must have function to start the server.
  • routes is the configuration array which is optional

After you create server.js file to start this dummy server on localhost you have to use nodemon (or maybe node in your case). To start the server simply run below command in the same file directory

nodemon server.js

For constant change tracking and automatic restart

OR

node server.js

For single time start

Points to Note here

  1. app : Return the express app instance on which you can directly start express backend coding if you want

  2. you must call/import json-dummy-api using require("json-dummy-api") as it uses Node at it Core

  3. startDummyServer Function : It is a function which actually starts listening the server. You can pass config routes as parameter if you want.

  4. Configroutes : its is an array of objects passed to startDummyServer() which is basically configuration for on which route which object to be sent

Random Data Types Config

| Data Import | Return values of | | ----------- | ------------------------------------------------------------------------------------------------------------------- | | PARAGRAPH | Returns the small parapgraph with avg 5 sentenses | | NAME | returns first name and last name combined | | SENTENSE | returns the sentense of avg 7 words | | ADDRESS | smallest possible address generator | | COUNTRY | Two Digit Short Country Code | | EMAIL | Generated random fake email with @ | | USERNAME | Generate Twitter username (random + fake) "@" included | | COMPANY | Generate fake company name to user | | DOMAIN | generate fake domain name for the comapany or the user | | PHONE | generates phone no | | DATE | generates date as a string (Long format) | | TIMESTAMP | Timestamp is 8 digit long number in ms | | NUMBER | generate random integer | | FLOAT | Random Float value between 0 to 1 | | BOOL | Returns either true or false | | NULL | returns null | | IMGURL | can pass single (as a side of sqaure) or two-arguments (width and hieght of the image) and it returns the image url |


All the above datatypes are exported from the module. you can simply import them via using Object Destructuring syntax. It regenrated random data for Mocking/Prototyping


Whats New in V2

V1 does not support Array and Object integration but the truth is JSON is incomplete without closely integrated Arrays and Objects in each others.For this problem set 2 new data config are added

| Data Import | Return values of | | ----------- | ------------------------------------------------------------------------------------ | | OBJECT | Returns the Empty Object by Default, can pass object key-value pairs as a Parameters | | ARRAY | returns the Array of type Specified of limit as length |

Use Cases

OBJECT

const { DATE, BOOL, OBJECT } = require("dummy-json-api");
const routes = [
  {
    routeName: "/object",
    response: {
      birthDay: OBJECT({
        date: DATE,
        adult: BOOL,
      }),
    },
  },
];
app.startDummyServer(routes);

in OBJECT function you can directly pass the childrens and its types as per above example

ARRAY

const {
  app,
  BOOL,
  ARRAY,
} = require("json-dummy-api");
const routes = [
  {
    routeName: "/array",
    response: {
      arr: ARRAY({
        type: BOOL,
        limit: 30,
      }),
    },
  },
];
app.startDummyServer(routes);

in ARRAY function you can directly pass object where you can specify type of the array elements in type and length of the array in limit args. Both args are compulsory

Default Routes

while developing version 1 I just wanted to add some default routes just like json-typicode api hence three default routes were added in V2

| Route | Value it returns | | ----------- | ------------------------------------------------------------------------------ | | /qanda | returns the array of the QandA with Question, Answer , Upvotes , Downvotes | | /users | return the array of user objects with Name,username,Bio,Age | | /products | returns the array of products with title, description,price,starrating,instock |

Above are all the parent routes like this routes usually returns all the data. To get one perticular object you have to pass the id in request as /users/25 above request will send only the data of user with userID 25

examples

/users/12
/users
/products/25
/qanda/14

v3 Specials :

In version 3 you can request any type of request to the server i.e GET, PUT,DELETE,POST

all you have to do is to specify reqType param in the route config object of the array.

reqType : GET,POST,PUT,DELETE