├── .gitignore ├── mock ├── blog │ ├── profile.js │ ├── comments.js │ └── posts.js ├── documents │ ├── index.js │ ├── query.js │ └── 基本.js ├── router.json ├── config.json ├── middlewares │ ├── _decodeChinese.js │ └── _postAsGet.js └── _db.js ├── README.md ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock -------------------------------------------------------------------------------- /mock/blog/profile.js: -------------------------------------------------------------------------------- 1 | module.exports = { name: "typicode" }; 2 | -------------------------------------------------------------------------------- /mock/documents/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | something: "blabla" 3 | }; 4 | -------------------------------------------------------------------------------- /mock/documents/query.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | something: "blablabla" 3 | }; 4 | -------------------------------------------------------------------------------- /mock/documents/基本.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | something: "blablablabla" 3 | }; 4 | -------------------------------------------------------------------------------- /mock/router.json: -------------------------------------------------------------------------------- 1 | { 2 | "/*/*/*": "/$1-$2-$3", 3 | "/*/*": "/$1-$2" 4 | } 5 | -------------------------------------------------------------------------------- /mock/blog/comments.js: -------------------------------------------------------------------------------- 1 | module.exports = [{ id: 1, body: "some comment", postId: 1 }]; 2 | -------------------------------------------------------------------------------- /mock/blog/posts.js: -------------------------------------------------------------------------------- 1 | module.exports = [{ id: 1, title: "json-server", author: "tydpicode" }]; 2 | -------------------------------------------------------------------------------- /mock/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "port": 9898, 3 | "routes": "./mock/router.json", 4 | "middlewares": [ 5 | "./mock/middlewares/_postAsGet.js", 6 | "./mock/middlewares/_decodeChinese.js" 7 | ], 8 | "delay": 800 9 | } 10 | -------------------------------------------------------------------------------- /mock/middlewares/_decodeChinese.js: -------------------------------------------------------------------------------- 1 | module.exports = function(req, res, next) { 2 | const regex = /%(\d|[A-Z]){2}/; 3 | const isMatch = regex.test(req.url); 4 | if (isMatch) { 5 | req.url = decodeURI(req.url); 6 | } 7 | next(); 8 | }; 9 | -------------------------------------------------------------------------------- /mock/middlewares/_postAsGet.js: -------------------------------------------------------------------------------- 1 | module.exports = function(req, res, next) { 2 | if (req.method === 'POST') { 3 | // Converts POST to GET and move payload to query params 4 | // This way it will make JSON Server that it's GET request 5 | req.method = 'GET'; 6 | req.query = req.body; 7 | } 8 | // Continue to JSON Server router 9 | next(); 10 | }; 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json-server-mulitple-files-sample 2 | 3 | 4 | ## Build 5 | `yarn` 6 | 7 | Install dependencies 8 | 9 | `yarn mock` 10 | 11 | Start the server 12 | 13 | 14 | ## Easily mock API: 15 | 16 | Add files: 17 | ``` 18 | └── mock 19 | └── hello 20 |    ├── world.js 21 |     └── index.js 22 | ``` 23 | And you'll be able to get data by visiting: 24 | 25 | `localhost:9898/hello/world` 26 | `localhost:9898/hello` 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-server-multiple-files-sample", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "mock": "nodemon --watch mock --exec 'json-server ./mock/_db.js -c ./mock/config.json'" 8 | }, 9 | "author": "Billyyyyy3320", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "glob": "^7.1.4", 13 | "json-server": "^0.15.0", 14 | "nodemon": "^1.19.1" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /mock/_db.js: -------------------------------------------------------------------------------- 1 | const Path = require("path"); 2 | const glob = require("glob"); 3 | const apiFiles = glob.sync(Path.resolve(__dirname, "./") + "/**/[!_]*.js", { 4 | nodir: true 5 | }); 6 | 7 | let data = {}; 8 | apiFiles.forEach(filePath => { 9 | const api = require(filePath); 10 | let [, url] = filePath.split("mock/"); 11 | url = 12 | url.slice(url.length - 9) === "/index.js" 13 | ? url.slice(0, url.length - 9) // remove /index.js 14 | : url.slice(0, url.length - 3); // remove .js 15 | data[url.replace(/\//g, "-")] = api; 16 | }); 17 | module.exports = () => { 18 | return data; 19 | }; 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Billyyyyy3320 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------