Node.js as a JSON forwarder
I'm new to Node.js but I wanted to use it as a quic开发者_StackOverflow中文版k web server which would simply take in a request uri and then run a query on an internal service which returns a JSON stream.
I.e. something like this:
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);
I'm using the newest stable version of node.js - version 0.4.12. I was hoping this would be very easy to do however, I havent been able to find some examples on the net as they all seem to use an old API version so I'm getting error after error.
Would anyone be able to provide a code solution to the above that works with the new Node APIs?
Thanks!
here is a code that should explain your task:
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);
You should use express, it would make it a lot easier.
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);
Ok since this question has a number of up-votes it would seem others are interested in it as well. While Nican's answer was the most helpful for getting actual JSON, I decided that my solution would use both the http lib and express as suggested by 3on as I really like its simplicity...
So for those interested my final solution was a combo of the two:
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
});
});
Very nice and neat. Thanks for the help all.
精彩评论