diff --git a/README.md b/README.md index c693a2b..c7a889e 100644 --- a/README.md +++ b/README.md @@ -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.* diff --git a/example/mock/.local-web-server.json b/example/mock/.local-web-server.json index e0ef555..1865ed2 100644 --- a/example/mock/.local-web-server.json +++ b/example/mock/.local-web-server.json @@ -45,6 +45,10 @@ { "route": "/five/:id\\?name=:name", "module": "/mocks/five.js" + }, + { + "route": "/users*", + "module": "/mocks/users.js" } ] } diff --git a/example/mock/mocks/users.js b/example/mock/mocks/users.js new file mode 100644 index 0000000..08bc7e9 --- /dev/null +++ b/example/mock/mocks/users.js @@ -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 diff --git a/jsdoc2md/README.hbs b/jsdoc2md/README.hbs index 59cb2b9..2e21209 100644 --- a/jsdoc2md/README.hbs +++ b/jsdoc2md/README.hbs @@ -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.* diff --git a/lib/middleware.js b/lib/middleware.js index d76c0db..c4b88be 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -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)}`) } }