2011-09-24 58 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 
     }); 
    }); 

非常好,整齐。感谢所有帮助。