将express.js路由function的结果传递给另一个路由function

我还在用node.js / express.jsfind我的脚。 我想将一个其他Web服务的结果传递给另一个…或者从两个Web服务函数创build一个新的路由服务…

函数A连接到mysql并构build一​​个json对象

例如

url:端口/ getMySQLRecord /:的recordId

函数B添加一个新的文档(JSON对象)到一个mongoDB集合

它接受一个AJAX POST

例如

url:端口/ insertMongoDoc

functionA和B目前都作为REST Web服务工作…(我怎样才能最好地把A的结果传给B?)

HTTP客户端调用A并将结果传递给B似乎效率不高。

我的意思是当服务器已经有对象数据时,使用2 x带宽似乎不是最好的select。

如果这是nix我会使用| …

//pseudocode loadRecord middleware just queries mysql for req.params.recordid // and stores the result as req.body, then calls next() //storeReqDotBodyInMongo just takes req.body and does a mongo insert, then calls next() //sendResponse is just res.send(req.body) app.get('/getMySQLRecord/:recordid', loadRecord, sendResponse); app.post('/insertMongoDoc', express.bodyParser(), storeReqDotBodyInMongo, sendResponse); app.get('/getMySQLAndInsertMongo/:record', loadRecord, storeReqDotBodyInMongo, sendResponse); 

请注意将中间件连接到unixpipe道的相似性。 而不是stdio他们使用req / res / next。