docs.. mock responses API example

This commit is contained in:
Lloyd Brookes
2015-11-27 21:54:33 +00:00
parent 6c4a9a2436
commit 383d48473c
5 changed files with 46 additions and 6 deletions

View File

@ -21,7 +21,7 @@ A simple web-server for productive front-end development. Typical use cases:
* Mocks are defined with config (static), or code (dynamic).
* CORS-friendly, all origins allowed by default.
* Proxy server
* Map local routes to remote services. Removes CORS issues with remote servers.
* Map local routes to remote servers. Removes CORS pain when consuming remote services.
* File sharing
**Requires node v4.0.0 or higher**.
@ -97,7 +97,7 @@ You're building a web app with client-side routing, so mark `index.html` as the
$ ws --spa index.html
```
By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
By default, typical SPA paths (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
*If a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*

View File

@ -45,6 +45,10 @@
{
"route": "/five/:id\\?name=:name",
"module": "/mocks/five.js"
},
{
"route": "/users*",
"module": "/mocks/users.js"
}
]
}

View File

@ -0,0 +1,34 @@
const users = [
{ id: 1, name: 'Lloyd', age: 40, nationality: 'English' },
{ id: 2, name: 'Mona', age: 34, nationality: 'Palestinian' },
{ id: 3, name: 'Francesco', age: 24, nationality: 'Italian' }
]
const mockResponses = [
{ request: { method: 'PUT' }, response: { status: 400 } },
{ request: { method: 'DELETE' }, response: { status: 400 } },
{
/* for GET requests, return a subset of data filtered on 'minAge' and 'nationality' */
request: { method: 'GET' },
response: function (ctx) {
ctx.body = users.filter(user => {
const meetsMinAge = user.age >= (Number(ctx.query.minAge) || 0)
const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
return meetsMinAge && requiredNationality
})
}
},
{
/* for POST requests, create a new user with the request data (a JSON user) */
request: { method: 'POST' },
response: function (ctx) {
const newUser = ctx.request.body
users.push(newUser)
newUser.id = users.length
ctx.status = 201
ctx.response.set('Location', `/users/${newUser.id}`)
}
}
]
module.exports = mockResponses

View File

@ -21,7 +21,7 @@ A simple web-server for productive front-end development. Typical use cases:
* Mocks are defined with config (static), or code (dynamic).
* CORS-friendly, all origins allowed by default.
* Proxy server
* Map local routes to remote services. Removes CORS issues with remote servers.
* Map local routes to remote servers. Removes CORS pain when consuming remote services.
* File sharing
**Requires node v4.0.0 or higher**.
@ -97,7 +97,7 @@ You're building a web app with client-side routing, so mark `index.html` as the
$ ws --spa index.html
```
By default, typical SPA urls (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
By default, typical SPA paths (e.g. `/user/1`, `/login`) would return `404 Not Found` as a file does not exist with that path. By marking `index.html` as the SPA you create this rule:
*If a static file at the requested path exists (e.g. `/css/style.css`) then serve it, if it does not (e.g. `/login`) then serve the specified SPA and handle the route client-side.*

View File

@ -101,12 +101,14 @@ function mockResponses (route, targets) {
}
if (target) {
debug('target response: %j', target.response)
debug('target response: %s', target.response)
if (t.isFunction(target.response)) {
const pathMatches = ctx.url.match(pathRe).slice(1)
target.response.apply(null, [ctx].concat(pathMatches))
} else {
} else if (t.isPlainObject(target.response)) {
Object.assign(ctx.response, target.response)
} else {
throw new Error(`Invalid response: ${JSON.stringify(target.response)}`)
}
}