express-mockjs
express mockjs api server
How to use it
Installation
$ npm install --save-dev express-mockjs
Examples
var path =var express =var mockjs =var app =// Using the default path /app// or custom path /apiapp// Add your middleware here, etc.app;
You can then access the http://localhost:3000/api to view API documents.
Recommended using nodemon to monitor auto restart services
Mock JSON
Examples
.
├── mocks
├── home
⎪ ├── data.json
├── user
⎪ ├── data.js
⎪ ├── data.json
├── game
├── data.json
data.json
Mock JSON
here is not a real JSON file, and more like a JS file, so you want to use the following format.
Hypothetical file in 'mocks/home/test.json'
/*** Interface function description** @url /api-access-path** Parameter description and other instructions.* uid: user ID* name: username* email: the email* etc.*/"code": 0"result|5":"uid|+1": 1"name": "@name""email": "@email"
Then you can access the http://localhost:3000/api/api-access-path through the browser.
Of course, you can also use the JS file directly.
/*** home page links** @url /home-links** Here you can write a detailed description* of the parameters of the information.*/moduleexports ={ // simulation error code, 1/10 probability of error code 1.return Math < 01 ? 1 : 0;}"list|5-10":"title": "@title" "link": "@url";
Or export function.
/*** user page - user info** @url /user?uid=233** GET: Request method and parameter* uid This is the requested userID** Here you can write a detailed description* of the parameters of the information.*/module {var uid = reqqueryuid;if !uidreturncode: -1msg: 'no uid'returncode: 0data:"uid": +uid"name": "@name""age|20-30": 1"email": "@email""date": "@date";};