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:
Without installing
(using npx
, shipped with npm@5.2.0
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
Usage: json-mock-api [options]
Options:
-v, --version output the version number
-c, --cors Enable CORS on al requests
-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
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
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
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.
Peter Goes (@petergoes) - petergoes.nl