Javascript Node.js作为JSON转发器

Javascript Node.js作为JSON转发器,javascript,json,web-services,node.js,Javascript,Json,Web Services,Node.js,我不熟悉Node.js,但我想将其用作一个快速web服务器,它只需接收请求uri,然后在返回JSON流的内部服务上运行查询 也就是说,类似这样的事情: http.createServer(function(request, response) { var uri = url.parse(request.url).pathname; if(uri === "/streamA") { //send get request to internal network server http

我不熟悉Node.js,但我想将其用作一个快速web服务器,它只需接收请求uri,然后在返回JSON流的内部服务上运行查询

也就是说,类似这样的事情:

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版本,所以我得到了一个又一个错误

是否有人能够提供一个与新节点API一起工作的上述代码解决方案

谢谢

你应该使用,这样会更容易

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

下面是一段代码,可以解释您的任务:

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

好的,因为这个问题有很多赞成票,其他人似乎也对它感兴趣。虽然Nican的回答对获取实际的JSON最有帮助,但我决定我的解决方案将使用3on建议的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 
        }); 
    });

非常漂亮整洁。谢谢大家的帮助。

谢谢大家的例子。Express看起来非常有用,但从阅读文档中可以清楚地看出我将如何从另一台服务器获取json…您可以使用请求,例如
request('http://',function(error,response,body){res.send(body)}
谢谢Nican,当我有一个像
path:'/db?'
这样的路径时,这就可以了,但是当我把它改成另一个像
path:'/db?{\'SQL\':\'SELECT*FROM classifier\u results\u summary\'}这样的路径时,
我得到一个套接字挂起-res没有定义,知道为什么吗?再次更新-使用路径:escape('myUrl'))函数以确保路径是URL安全的…使用的转义函数是通用javascript库函数之一-请参阅此处的doco,我会非常小心地处理该站点中的任何内容。@Linus,是的,这显然不是prod代码。谢谢你的提示。我们在JS中进行所有格式化,并在节点端重建查询以避免这些问题。Node非常适合这项任务,这意味着我们可以轻松地将infra移动到任何需要的地方。