I'm using nock to intercept calls to my API host and do some local db look-ups before returning the response.
Please refer the below code. I intercept calls to 'entrypoint' and would like to reply with the data fetched from local datastore.
I believe this is a problem with nock module itself and I heard few suggestions to use streams. Can you please help overcome this problem?
// ... Code Block
var nock = require('nock'),
request = require('request'),
DataStore = require('nedb'),
db = new DataStore({filename: './nedb.data'});
db.loadDatabase(function(err) {
if (err) throw err;
});
db.insert ({'record1': { "key1" : "value1"} });
db.insert ({'record2': { "key2" : "value2"} });
db.insert ({'record3': { "key3" : "value3"} });
db.insert ({'record4': { "key4" : "value4"} });
db.insert ({'record5': { "key5" : "value5"} });
var n = nock('http://localhost:8000')
.get ('/entrypoint')
.reply (200, function () {
db.find ({}, function(err, docs) {
if (err) throw err;
return docs;
});
});
request('http://localhost:8000/entrypoint', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log ('\nBEGIN: Body: ');
console.log(body);
console.log ('\n\nEND: Body: \n\n');
}
});
It is not possible at the moment, I have created this bug to start fixing this problem.