From 7f835fe88df7c85727464074adfaebc3100aa028 Mon Sep 17 00:00:00 2001 From: Dan Hough Date: Fri, 14 Mar 2014 22:44:26 +0100 Subject: [PATCH] Updated readme with useful example --- .gitignore | 3 ++- readme.md | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8f1c4dc..c62e151 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ logs results node_modules -npm-debug.log \ No newline at end of file +npm-debug.log +.DS_Store diff --git a/readme.md b/readme.md index 9bc7e41..e9cdb37 100644 --- a/readme.md +++ b/readme.md @@ -54,7 +54,33 @@ Run `interfake -?` for a full list of command-line options. ----- -Interfake allows for more complex API structures, post-response endpoints and two different methods of mocking up new endpoints: by loading the file as above, or on-the-fly using an HTTP meta-API. +Interfake allows for more complex API structures, post-response endpoints and three different methods of mocking up new endpoints: the JavaScript API (useful for tests), by loading a file (like above), or on-the-fly using an HTTP meta-API. + +## Method 3: JavaScript + +Make sure you've install Interfake as a local module using `npm install interfake --save`. Then, you can start doing things like this: + +```javascript +var interfake = require('interfake'); +var request = require('request'); // Let's use request for this example + +interfake.createRoute({ + request: { + url: '/endpoint', + method: 'get' + }, + response: { + code: 200, + body: {} + } +}); + +interfake.listen(3030); // The server listens on port 3030 + +request('http://localhost:3030/endpoint', function (error, response, body) { + console.log(response.statusCode); // prints 200 +}); +``` ## Method 1: JSON File