|
1 |
| -A plugin package to do mock response with the help of AnyProxy 4.0, the basic theory is fork from the [dora-plugin-proxy](https://github.com/dora-js/dora-plugin-proxy). |
| 1 | +A plugin package to do mock response with the help of AnyProxy 4.0, the basic idea is fork from the [dora-plugin-proxy](https://github.com/dora-js/dora-plugin-proxy). |
| 2 | + |
| 3 | +# Install |
| 4 | +Install the module through AnyProxy package management, you can find this package through `mock-response`. |
| 5 | + |
| 6 | +# How to use |
| 7 | +You can edit the configuration in AnyProxy directy, a demo configuration file could be like this: |
| 8 | + |
| 9 | +```javascript |
| 10 | + |
| 11 | +module.exports = { |
| 12 | + // mock response |
| 13 | + 'remote/demo.json': function (req, res) { |
| 14 | + setTimeout(() => { |
| 15 | + res.json({ |
| 16 | + stat: 'ok', |
| 17 | + data: [] |
| 18 | + }) |
| 19 | + }, 300); |
| 20 | + }, |
| 21 | + // map local, replace the remote file.js with local file |
| 22 | + 'remote/file.js': 'fullpath/of/local/file.js', |
| 23 | + // inverse proxy |
| 24 | + 'example.com/remote/path': 'http://localhost/local/path' |
| 25 | +} |
| 26 | + |
| 27 | +``` |
| 28 | + |
| 29 | +## Mock response |
| 30 | +Mock response for specified urls, you can simulate a network delay with `setTimeout`. In the related resposne function, this module exports some meta data and method for your convenience. |
| 31 | + |
| 32 | +```javascript |
| 33 | +/* |
| 34 | + the function to do the mock thing |
| 35 | + @param req the request data |
| 36 | + {req.url} full url of the request |
| 37 | + {req.protocol} |
| 38 | + {req.headers} the headers of the request |
| 39 | + {req.body} the buffer body of the ruequest |
| 40 | + @param res a util to do response, it contains some handy functions |
| 41 | + {req.json(object)} response a json object |
| 42 | + {req.set(object|{key, value})} set reponse headers |
| 43 | + {req.type(json|html|text|png)} set the mime types |
| 44 | + {req.status(statusCode)} set status code |
| 45 | + {req.jsonp(json[, callbacQueryName])} return a jsonp |
| 46 | + {req.end(string|object)} response the data |
| 47 | +
|
| 48 | +*/ |
| 49 | +function (req, res) { |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +``` |
| 54 | + |
| 55 | +## Map local |
| 56 | + |
| 57 | +Map the remote file path with local file, useful when want to proxy a remote file |
| 58 | +```javascript |
| 59 | +'remote/file.js': 'fullpath/of/local/file.js' |
| 60 | +``` |
| 61 | + |
| 62 | +## Inverse proxy |
| 63 | + |
| 64 | +Map remote request to another target |
| 65 | + |
| 66 | +```javascript |
| 67 | +// |
| 68 | +'example.com/remote/path': 'http://localhost/local/path' |
| 69 | + |
| 70 | +``` |
0 commit comments