admin管理员组文章数量:1291027
I'm new to Node.js but I wanted to use it as a quick 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!
I'm new to Node.js but I wanted to use it as a quick 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!
Share Improve this question edited Sep 24, 2011 at 14:55 NightWolf asked Sep 24, 2011 at 14:47 NightWolfNightWolf 7,79411 gold badges75 silver badges122 bronze badges 03 Answers
Reset to default 6here is a code that should explain your task:
var http = require('http');
var url = require('url')
var options = {
host: 'www.google.',
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 bo 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.
本文标签: javascriptNodejs as a JSON forwarderStack Overflow
版权声明:本文标题:javascript - Node.js as a JSON forwarder - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741512761a2382702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论