2011-09-24 21 views
7

Tôi mới sử dụng Node.js nhưng tôi muốn sử dụng nó làm máy chủ web nhanh chóng chỉ cần yêu cầu uri và sau đó chạy truy vấn trên dịch vụ nội bộ trả về JSON suối.Node.js là một giao nhận JSON

I.e. một cái gì đó như thế này:

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

Tôi đang sử dụng phiên bản ổn định mới nhất của node.js - phiên bản 0.4.12. Tôi đã hy vọng điều này sẽ rất dễ dàng để làm tuy nhiên, tôi havent đã có thể tìm thấy một số ví dụ trên mạng vì tất cả họ dường như sử dụng một phiên bản API cũ vì vậy tôi nhận được lỗi sau khi lỗi.

Có ai có thể cung cấp giải pháp mã cho các giải pháp trên hoạt động với API Node mới không?

Cảm ơn!

Trả lời

6

đây là một mã mà nên giải thích nhiệm vụ của bạn:

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

Cảm ơn Nican, nó hoạt động khi tôi có một đường dẫn như' path: '/ db?' 'nhưng khi Tôi thay đổi điều này thành một đường dẫn khác như thế này 'path:'/db? {\ "SQL \": \ "CHỌN * FROM classifier_results_summary \"} ''Tôi nhận được một socket treo lên - res không được định nghĩa, bất kỳ ý tưởng tại sao ? – NightWolf

+2

Cập nhật lại - sử dụng đường dẫn: escape ('myUrl') để đảm bảo đường dẫn là URL an toàn ... – NightWolf

+0

Chức năng thoát được sử dụng là thư viện javascript chung - xem doco tại đây http://www.w3schools.com /jsref/jsref_escape.asp – NightWolf

3

Bạn nên sử dụng express, việc này sẽ dễ dàng hơn nhiều.

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

Cảm ơn ví dụ. Express trông rất hữu ích, nhưng từ đọc doco của nó rõ ràng như bùn như thế nào tôi sẽ acutally làm một json nhận được từ một máy chủ .... – NightWolf

+0

Bạn chỉ có thể sử dụng yêu cầu https://github.com/mikeal/request như vậy ' request ('http: //', function (error, response, body) {res.send (body)}) ' – 3on

2

Ok kể từ khi câu hỏi này có một số up-phiếu dường như những người khác quan tâm đến nó là tốt. Trong khi câu trả lời của Nican là hữu ích nhất để nhận JSON thực tế, tôi quyết định rằng giải pháp của tôi sẽ sử dụng cả http lib và express như được đề xuất bởi 3on vì tôi thực sự thích sự đơn giản của nó ...

Vì vậy, đối với những người quan tâm giải pháp cuối cùng của tôi là kết hợp của hai:

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

Rất đẹp và gọn gàng. Cảm ơn bạn đã giúp đỡ tất cả.

Các vấn đề liên quan