2011-09-24 38 views
7

我是新來的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一起工作嗎?

謝謝!

回答

6

這裏應該說明你的任務代碼:

var http = require('http'); 
var url = require('url') 

var options = { 
    host: 'www.google.com', 
    port: 80, 
    path: '/' //Make sure path is escaped 
}; //Options for getting the remote page 

http.createServer(function(request, response) { 
    var uri = url.parse(request.url).pathname; 
    if(uri === "/streamA") { 
    http.get(options, function(res) { 

     res.pipe(response); //Everything from the remote page is written into the response 
     //The connection is also auto closed 

    }).on('error', function(e) { 
     response.writeHead(500); //Internal server error 
     response.end("Got error " + e); //End the request with this message 
    }); 

    } else { 
    response.writeHead(404); //Could not find it 
    response.end("Invalid request"); 

    } 

}).listen(8080); 
+0

感謝Nican,當我有一個像'path:'/ db?我改變這個不同的路徑,像這樣一個路徑:'/ db?{\「SQL \」:\「SELECT * FROM classifier_results_summary \」}''我得到一個套接字掛斷 - res沒有定義,任何想法爲什麼? – NightWolf

+2

再次更新 - 使用路徑:escape('myUrl')函數來確保路徑是URL安全的... – NightWolf

+0

使用的轉義函數是通用JavaScript庫之一 - 請參閱doco here http://www.w3schools.com /jsref/jsref_escape.asp – NightWolf

3

您應該使用express,它會使它更容易。

var app = express.createServer(); 

app.get('/json1', function(req, res){ 
    var json // = 
    res.send(json); 
}); 

app.get('/json2', function(req, res){ 
    var json // = 
    res.send(json); 
}); 

app.listen(8000); 
+0

謝謝你的例子。 Express看起來非常有幫助,但是從閱讀doco它清晰如泥,我會如何從另一臺服務器上執行json ... – NightWolf

+0

您可以使用請求https://github.com/mikeal/request這樣'請求('http://',函數(error,response,body){res.send(body)})' – 3on

2

好,因爲這個問題有很多了,票又好像別人有興趣它。雖然Nican的回答對於獲得實際的JSON最有幫助,但我決定我的解決方案將使用http3b提供的http lib和express,因爲我真的很喜歡它的簡單性...

因此對於那些感興趣的人,我的最終解決方案是兩個組合:

var http = require("http"), 
    url = require("url"); 

var app = require('express').createServer();  
app.listen(9898); 

var query = {"SQL" : "SELECT * FROM classifier_results_summary"}; 
var options = { 
    host: 'issa', 
    port: 9090, 
    path: '/db?'+ escape(JSON.stringify(query)) 
}; //Options for getting the remote page 

    app.get('/stream/:id', function(req, res){ 
     //console.log(options.path); 
     http.get(options, function(response) { 
     response.pipe(res); //Everything from the remote page is written into the response 
      //The connection is also auto closed 
     }).on('error', function(e) { 
      response.writeHead(500); //Internal server error 
      response.end("Got error " + e); //End the request with this message 
     }); 
    }); 

非常好,整齊。感謝所有幫助。