2013-09-10 91 views
1

我仍然在使用node.js/express.js找到自己的腳。我想一個REST Web服務的結果傳遞給另一個...或創建兩個Web服務功能的新的路由服務...將express.js路由功能的結果傳遞給另一個路由功能

功能的連接到MySQL,並建立一個JSON對象

例如

URL:端口/ getMySQLRecord /:的recordId

函數B添加新文檔(JSON對象),以一個集合的MongoDB

它接受一個AJAX POST

例如

網址:端口/ insertMongoDoc

功能A和B目前的工作作爲REST Web服務......(我怎麼能最好的管道A的結果到B?)

似乎效率不高的HTTP客戶端調用A並將結果傳遞給B.

我的意思是當服務器已經有對象數據時使用2 x帶寬似乎不是最佳選擇。

如果這是nix我會使用| ...

回答

1
//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); 

注意將中間件連接到unix管道的相似性。他們使用req/res/next來代替stdio。

+0

好的我知道了!感謝你的解釋。 第一兩條路線(已經)在我的server.js(圖分離的.js文件可以說mysqlfunctions.js和mongofunctions.js,但是這並不重要) server.js確實與最終的路徑路由。 ..讓我覺得cutGrass並創建fertiliseGrass這些割草機之一 - 的表土疏鬆;) 'app.get( '/ MULCH /:的recordId',cutGrass,fertiliseGrass,sendResponse)' 感謝彼得。 – urfx

相關問題