{"id":16594564,"url":"https://github.com/hjzheng/mock-server","last_synced_at":"2025-04-15T03:55:17.236Z","repository":{"id":57233868,"uuid":"113140061","full_name":"hjzheng/mock-server","owner":"hjzheng","description":"front-end  mock server, easy to use, inspired by wiremock","archived":false,"fork":false,"pushed_at":"2019-04-04T02:08:10.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T15:45:50.358Z","etag":null,"topics":["frontend","mock"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/fe-mock-server","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hjzheng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-05T06:03:31.000Z","updated_at":"2019-04-04T02:08:12.000Z","dependencies_parsed_at":"2022-08-27T13:54:26.964Z","dependency_job_id":null,"html_url":"https://github.com/hjzheng/mock-server","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hjzheng%2Fmock-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hjzheng%2Fmock-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hjzheng%2Fmock-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hjzheng%2Fmock-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hjzheng","download_url":"https://codeload.github.com/hjzheng/mock-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248537192,"owners_count":21120711,"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":["frontend","mock"],"created_at":"2024-10-11T23:46:39.814Z","updated_at":"2025-04-15T03:55:17.219Z","avatar_url":"https://github.com/hjzheng.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mock-server\n\n[![npm version](https://img.shields.io/npm/v/fe-mock-server.svg?style=flat-square)](https://www.npmjs.com/package/fe-mock-server)\n[![npm downloads](https://img.shields.io/npm/dt/fe-mock-server.svg?style=flat-square)](https://www.npmjs.com/package/fe-mock-server)\n[![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu)\n\n### Usage\n\n```shell\nnpm install fe-mock-server -g\n```\n\n```shell\nfe-mock-server --mock \"./config/\" --api-prefix \"/api\" --port 8989\n```\n\n- --mock:  mock files's location\n- --api-prefix: api prefix, default '/'\n- --port: server port, default 8989\n\nAbout mock configuration file, please check [config](https://github.com/hjzheng/mock-server/tree/master/conf).\n\n\n### Example\n\n```\nvar _ = require('lodash');\n// 简单的 RESTful\nmodule.exports = function(configurations) {\n\tvar users = [\n\t\t{id: 1, name: 'hurry', grade: 90},\n\t\t{id: 2, name: 'hjzheng', grade: 88},\n\t\t{id: 3, name: 'Jack', grade: 30},\n\t\t{id: 4, name: 'Tom', grade: 70},\n\t\t{id: 5, name: 'Bell', grade: 40}\n\t];\n\n\tconfigurations.add([\n\t\t{\n\t\t\trequest: {\n\t\t\t\tmethod: 'GET',\n\t\t\t\turlPattern: '/users'\n\t\t\t},\n\t\t\tresponse: {\n\t\t\t\tstatus: 200,\n\t\t\t\tbody: function() {\n\t\t\t\t\treturn users;\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json'\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\trequest: {\n\t\t\t\tmethod: 'GET',\n\t\t\t\turlPattern: '/users/:id'\n\t\t\t},\n\t\t\tresponse: {\n\t\t\t\tstatus: 200,\n\t\t\t\tbody: function(req) {\n\t\t\t\t\treturn _.find(users,\n\t\t\t\t\t{ 'id': parseInt(req.params.id, 10)});\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json'\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\trequest: {\n\t\t\t\tmethod: 'DELETE',\n\t\t\t\turlPattern: '/users/:id'\n\t\t\t},\n\t\t\tresponse: {\n\t\t\t\tstatus: 200,\n\t\t\t\tbody: function(req) {\n\t\t\t\t\t_.remove(users, function(u) {\n\t\t\t\t\t\treturn u.id === parseInt(req.params.id, 10);\n\t\t\t\t\t});\n\t\t\t\t\treturn {success: true};\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json'\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\trequest: {\n\t\t\t\tmethod: 'POST',\n\t\t\t\turlPattern: '/users'\n\t\t\t},\n\t\t\tresponse: {\n\t\t\t\tstatus: 200,\n\t\t\t\tbody: function(req) {\n\t\t\t\t\treq.body.id = users[users.length - 1].id + 1;\n\t\t\t\t\tusers.push(req.body);\n\t\t\t\t\treturn {success: true};\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json'\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\trequest: {\n\t\t\t\tmethod: 'PUT',\n\t\t\t\turlPattern: '/users/:id'\n\t\t\t},\n\t\t\tresponse: {\n\t\t\t\tstatus: 200,\n\t\t\t\tbody: function(req) {\n\t\t\t\t\treq.body.id = parseInt(req.params.id, 10);\n\t\t\t\t\tvar index = _.findIndex(users,\n\t\t\t\t\t    { 'id': parseInt(req.params.id, 10)})\n\t\t\t\t\tusers[index] = req.body;\n\t\t\t\t\treturn {success: true};\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json'\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t]);\n}\n```\n\n### 数据配置模式简单说明\n\n\u003e 通过数据，简化mock配置过程\n\n#### 1. mock简单模式，模块文件导出一个对象\n\n+ 针对 get 请求，可以直接按照 `[url]: data` 的形式配置\n```javascript\nmodule.exports = {\n\t'/test': {\n\t\tname: 'aa',\n\t\tage: 'bb',\n\t\tgender: 'cc'\n\t},\n\t'/test2': 'This is data',\n\t'/test3': [1, 2, 3, '我是一条锦鲤']\n}\n```\n\n+ 针对非 get 请求（支持 get/post/delete/put ）,按照 `[url]: { [method]: data }` 的形式配置\n```javascript\nmodule.exports = {\n\t'/test':{\n\t\tpost: {\n\t\t\tmessage: 'message from post'\n\t\t},\n\t\tdelete: {},\n\t\tget: 'lalala'\n\t}\n}\n```\n\n+ 针对高级请求，需要对 request 和 response 做处理的，按照 `[url]: (req, res) =\u003e {}`的形式配置\n\n```javascript\nmodule.exports = {\n\t'/test': function(req, res) {\n\t\tconst query = req.query;\n\t\t// 设置 header\n\t\tres.set('Content-Type', 'text/html');\n\t\t// 设置 返回状态\n\t\tres.status(500);\n\t\treturn { message: 'This is internal error' }\n\t},\n\t'/test2': {\n\t\tget(req, res) {\n\t\t\treturn req.query;\n\t\t},\n\t\tpost(req, res) {\n\t\t\treturn req.params;\n\t\t}\n\t}\n}\n```\n\n#### 2. mock 兼容模式\n\u003e 在原有的配置方式中，可以兼容简单的配置模式。\n\n```javascript\nmodule.exports = function (configurations) {\n\tconfigurations.add([\n\t\t{\n\t\t\trequest: {\n\t\t\t\tmethod: 'GET',\n\t\t\t\turlPattern: '/tt/test'\n\t\t\t},\n\t\t\tresponse: {\n\t\t\t\tstatus: 200,\n\t\t\t\tbody: function () {\n\t\t\t\t\treturn [];\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json'\n\t\t\t\t}\n\t\t\t}\n\t\t},{\n\t\t\t'/test3': {\n\t\t\t\tmsg: '兼容模式'\n\t\t\t},\n\t\t\t'/test33': {\n\t\t\t\tpost: {}\n\t\t\t}\n\t\t}\n\t])\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhjzheng%2Fmock-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhjzheng%2Fmock-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhjzheng%2Fmock-server/lists"}