I have recently started to develop a node project where I am promised that all of the information that I will need to render a page will be exposed via API calls. This is fantastic because it significantly simplifies my development as it is easy to call a bunch of services and manipulate data.
The problem is that the services that I am promised to develop my project have not been created yet. Worse yet, the delivery of these services look like it may be weeks from now, but I need to start my development on the front end side of things now!
We’re using node to power our web server, specifically the express web framework and so it seemed only natural to be able to mock our web services within node. Ideally, I should be able to swap my mocked services with the real services in a relatively painless fashion (via a configuration parameter, perhaps?)
flatiron’s offering of nock seems like a brilliant idea. What it does is intercept every HTTP request to a given endpoint and returns your mocked response instead. That, alone, makes nock pretty cool. But wait! There’s more!
nock also provides a way to record any http request and then save the response as a nock mocked object. You just turn on the recorder, make your http request, and then you will have all the code necessary to “nock” it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var nock = require('nock'), request = require('request'), url = 'http://www.google.com/search?q=google'; nock.recorder.rec(true); request(url, function(error, response, body) { if (error) { // uh oh, there was an error } else { // yay, the request was a success! var nockCalls = nock.recorder.play(); console.log(nockCalls); } }); |
That’s AH-MAZ-ING!
n http://tadalafil-ph24.com/ us cialis generic