我是新來的Node.js,但我想用它作爲一個快速的Web服務器,它只需要一個請求uri,然後對返回JSON的內部服務運行查詢流。Node.js作爲JSON轉發器
I.e.像這樣:
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
if(uri === "/streamA") {
//send get request to internal network server http://server:1234/db?someReqA -->Returns JSON ....?
//send response to requestor of the JSON from the above get ....?
}
else if(uri === "/streamB") {
//send get request to internal network server http://server:1234/db?someReqB -->Returns JSON ....?
//send response to requestor of the JSON from the above get....?
}.listen(8080);
我正在使用最新的穩定版本的node.js - 版本0.4.12。我希望這會很容易做到,但是我還沒有能夠在網上找到一些例子,因爲它們都似乎使用舊的API版本,所以我錯誤後得到錯誤。
任何人都可以提供上述的代碼解決方案,與新的Node API一起工作嗎?
謝謝!
感謝Nican,當我有一個像'path:'/ db?我改變這個不同的路徑,像這樣一個路徑:'/ db?{\「SQL \」:\「SELECT * FROM classifier_results_summary \」}''我得到一個套接字掛斷 - res沒有定義,任何想法爲什麼? – NightWolf
再次更新 - 使用路徑:escape('myUrl')函數來確保路徑是URL安全的... – NightWolf
使用的轉義函數是通用JavaScript庫之一 - 請參閱doco here http://www.w3schools.com /jsref/jsref_escape.asp – NightWolf