Node.js – Nock和nedb – 在callback完成之前执行回复

我使用nock来拦截对我的API主机的调用,并在返回响应之前做一些本地数据库查找。

请参考下面的代码。 我拦截到“入口点”的调用,并希望回答从本地数据存储区获取的数据。

我相信这是一个nock模块本身的问题,我听到一些build议使用stream。 你能帮助克服这个问题吗?

// ... 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'); } }); 

目前不可能,我已经创build了这个bug来开始解决这个问题。

https://github.com/pgte/nock/issues/283