我是新来的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一起工作吗?
谢谢!
感谢Nican,当我有一个像'path:'/ db?我改变这个不同的路径,像这样一个路径:'/ db?{\“SQL \”:\“SELECT * FROM classifier_results_summary \”}''我得到一个套接字挂断 - res没有定义,任何想法为什么? – NightWolf
再次更新 - 使用路径:escape('myUrl')函数来确保路径是URL安全的... – NightWolf
使用的转义函数是通用JavaScript库之一 - 请参阅doco here http://www.w3schools.com /jsref/jsref_escape.asp – NightWolf