{"id":21945251,"url":"https://github.com/52cik/express-mockjs","last_synced_at":"2025-04-22T21:13:25.213Z","repository":{"id":87165106,"uuid":"69417756","full_name":"52cik/express-mockjs","owner":"52cik","description":"mockjs api middleware for Express","archived":false,"fork":false,"pushed_at":"2017-11-30T08:22:38.000Z","size":30,"stargazers_count":50,"open_issues_count":5,"forks_count":12,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-22T21:13:20.774Z","etag":null,"topics":["express","express-mockjs","mockjs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/52cik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-28T02:31:00.000Z","updated_at":"2023-08-14T07:44:22.000Z","dependencies_parsed_at":"2023-05-29T09:00:25.431Z","dependency_job_id":null,"html_url":"https://github.com/52cik/express-mockjs","commit_stats":{"total_commits":26,"total_committers":3,"mean_commits":8.666666666666666,"dds":0.07692307692307687,"last_synced_commit":"92252c95724f310f51eb65b6bc1eb274a923bd68"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/52cik%2Fexpress-mockjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/52cik%2Fexpress-mockjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/52cik%2Fexpress-mockjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/52cik%2Fexpress-mockjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/52cik","download_url":"https://codeload.github.com/52cik/express-mockjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250324708,"owners_count":21411946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","express-mockjs","mockjs"],"created_at":"2024-11-29T04:18:05.813Z","updated_at":"2025-04-22T21:13:25.191Z","avatar_url":"https://github.com/52cik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-mockjs\n\n\u003e express mockjs api middleware for Express\n\n[![Linux Build][travis-image]][travis-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![Dependencies][dependencies-image]][dependencies-url]\n[![node][node-image]][node-url]\n[![license MIT][license-image]][license-url]\n\n\n[中文文档](README.zh-CN.md)\n\n\n## How to use it\n\n### Installation\n\n``` sh\n$ npm install --save-dev express-mockjs\n```\n\n----\n\n### Quick start\n\n\n1. Create a directory `api-server`, then create the file `app.js`, the content is：\n\n``` js\nvar path = require('path')\nvar express = require('express')\nvar mockjs = require('express-mockjs')\n\nvar app = express()\n\n// Use the default path '/' (Not recommended)\n// app.use(mockjs(path.join(__dirname, 'mocks')))\n\n// Use a custom path '/api'\napp.use('/api', mockjs(path.join(__dirname, 'mocks')))\n\n// Here you can add any code.\n\napp.listen(3000);\n```\n\n2. Create a `mocks` directory under `api-server` and create `data.json` as follows:\n\n```js\n/**\n * api interface description\n *\n * @url /test-api\n */\n{\n  \"code\": 0,\n  \"result|5\": [\n    {\n      \"uid|+1\": 1,\n      \"name\": \"@name\",\n      \"email\": \"@email\"\n    }\n  ]\n}\n```\n\n3. Install dependent modules\n\n```sh\n$ npm i -S express express-mockjs\n```\n\n4. Start\n\n```sh\n$ node app.js\n# or\n$ nodemon app.js\n```\n\n\u003e You can then access the \u003chttp://localhost:3000/api\u003e to view API documents.\n\n**Recommended using [nodemon][nodemon] to monitor auto restart services**\n\n----\n\n### Mock JSON\n\n* [Mock.js 0.1 doc](https://github.com/nuysoft/Mock/wiki)  \n* [Mock Sample](http://mockjs-lite.js.org/docs/examples.html)  \n\n\n### Examples\n\n```\n.\n├── mocks\n    ├── home\n    ⎪   ├── data.json\n    ├── user\n    ⎪   ├── data.js\n    ⎪   ├── data.json\n    ├── game\n        ├── data.json\n```\n\n\n\n## data.json\n\n`Mock JSON` here is not a real JSON file, and more like a JS file, so you want to use the following format.\n\n\u003e Hypothetical file in 'mocks/home/test.json'\n\n``` js\n/**\n * Interface function description\n *\n * @url /api-access-path\n *\n * Parameter description and other instructions.\n * uid: user ID\n * name: username\n * email: the email\n * etc.\n */\n\n{\n  \"code\": 0,\n  \"result|5\": [\n    {\n      \"uid|+1\": 1,\n      \"name\": \"@name\",\n      \"email\": \"@email\"\n    }\n  ]\n}\n```\n\nThen you can access the \u003chttp://localhost:3000/api/api-access-path\u003e through the browser.\n\nOf course, you can also use the JS file directly.\n\n``` js\n/**\n * home page links\n *\n * @url /home-links\n *\n * Here you can write a detailed description\n * of the parameters of the information.\n */\n\nmodule.exports = {\n  \"code\": function () { // simulation error code, 1/10 probability of error code 1.\n    return Math.random() \u003c 0.1 ? 1 : 0;\n  },\n  \"list|5-10\": [\n    {\"title\": \"@title\", \"link\": \"@url\"}\n  ]\n};\n```\n\nOr export function.\n\n``` js\n/**\n * user page - user info\n *\n * @url /user?uid=233\n *\n * GET: Request method and parameter\n *   uid This is the requested userID\n *\n * Here you can write a detailed description\n * of the parameters of the information.\n */\n\nmodule.exports = function (req) {\n  var uid = req.query.uid;\n\n  if (!uid) {\n    return {\n      code: -1,\n      msg: 'no uid',\n    }\n  }\n\n  return {\n    code: 0,\n    data: {\n      \"uid\": +uid,\n      \"name\": \"@name\",\n      \"age|20-30\": 1,\n      \"email\": \"@email\",\n      \"date\": \"@date\",\n    },\n  };\n};\n```\n\n\n\n[travis-url]: https://travis-ci.org/52cik/express-mockjs\n[travis-image]: https://img.shields.io/travis/52cik/express-mockjs/master.svg?label=linux\n\n[coveralls-url]: https://coveralls.io/github/52cik/express-mockjs?branch=master\n[coveralls-image]: https://coveralls.io/repos/52cik/express-mockjs/badge.svg?branch=master\u0026service=github\n\n[license-url]: https://opensource.org/licenses/MIT\n[license-image]: https://img.shields.io/badge/license-MIT-blue.svg\n\n[dependencies-url]: https://david-dm.org/52cik/express-mockjs\n[dependencies-image]: https://img.shields.io/david/52cik/express-mockjs.svg?style=flat\n\n[node-url]: https://nodejs.org\n[node-image]: https://img.shields.io/badge/node-%3E%3D%200.10.0-brightgreen.svg\n\n\n[nodemon]: https://nodemon.io\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F52cik%2Fexpress-mockjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F52cik%2Fexpress-mockjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F52cik%2Fexpress-mockjs/lists"}