Skip to content

coderuse/json-mock-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Json Mock Api npm version

Mock an api with plain json files. This simple CLI tool allows you to turn a folder of static json files into a mock api server.


Table of Contents:

Usage

Without installing (using npx , shipped with [email protected] and higher)

npx json-mock-api --port 3000 --directory .

or

Add json-mock-api to your project ...

npm install --save json-mock-api

... then update your package.json ...

{
    "scripts": {
+       "mock": "json-mock-api --port 3000 --directory ."
    }
}

... and finally run the command

npm run mock

Options

Usage: json-mock-api [options]

Options:

  -v, --version             output the version number
  -d, --directory [path]    Directory (default: .)
  -m, --middleware <files>  Expressjs middleware (default: )
  -p, --port [number]       Port (default: 3000)
  -h, --help                output usage information

Examples:

  $ json-mock-api --middleware ./middleware-1.js,./middleware-2.js

The Json files

Given this file structure:

.
└── api/
    ├── login.json
    ├── product/
    │   └── index.json
    ├── user/
    │   └── 1.json
    └── users.json

That results in the following endpoints:

http://localhost:3000/api/login
http://localhost:3000/api/products
http://localhost:3000/api/user/1
http://localhost:3000/api/users

Handling different HTTP Verbs

You can specify a HTTP Verb in the json file name like so:

.
└── api/
    └── user/
        ├── 1.json
        ├── 1.post.json
        └── 1.put.json

When you access the endpoint http://localhost:3000/api/user/1 via:

  • a POST request, the file ./api/user/1.post.json is returned
  • a PUT request, the file ./api/user/1.put.json is returned
  • any other verb (GET, DELETE, ...), the file ./api/user/1.json is returned

Custom middleware

You can run your own ExpressJS middleware if you want to.

To load your own middleware, use the -m or --middleware flags:

json-mock-api --middlware ./middlware-1.js,./middleware-2.js

The above command will load the files middleware-1.js and middleware-2.js from the current working directory and use them in this order when a request is made before the response is send to the user.

You could use your own middleware to, for example, add authentication.

Author

Peter Goes (@petergoes) - petergoes.nl

License

MIT

About

Mock an api with plain json files

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.6%
  • Shell 0.4%