2013-02-14 64 views
1

我真的很喜歡用幾乎所有的語言來理解範圍和其他本質的東西。現在我正在構建一個快速應用程序,它接受用戶輸入,然後查詢任意api,然後將其提供給控制檯。爲了處理其餘的API,我正在使用Shred。我知道我可以使用get請求中建立的節點,但由於某種原因,我永遠無法使它工作。用戶向我的應用程序發出以下請求,/ query?query =。這是我現在擁有的。我無法真正描述我在做什麼,所以請閱讀代碼評論。有關範圍,node.js和express的問題

var http = require('http'); 
var Shred = require("shred"); 
var assert = require("assert"); 

exports.query = function(req, res){ 
    //thequery is the string that is requested 
    var thequery = req.query.query; 
    var shred = new Shred(); 


    console.log("user searched" + " " + thequery); 
    console.log(); 

    //The if statement detects if the user searched a url or something else 
    if (thequery.indexOf("somearbitratyrestapi.com") !== -1){ 
     console.log("a url was searched"); 
     //find info on the url 

     var thedata = shred.get({ 
      url: "http://somearbitratyrestapi.com/bla/v2" + thequery, 
      headers: { 
      Accept: "application/json" 
      }, 
      on: { 
      // You can use response codes as events 
      200: function(response) { 
       // Shred will automatically JSON-decode response bodies that have a 
       // JSON Content-Type 

       //This is the returned json 
        //I want to get this json Data outside the scope of this object 
       console(response.content.body); 

      }, 

      // Any other response means something's wrong 
      response: function(response) { 
      console.log("ohknowz"); 
      } 
      } 
     }); 

      //I want to be able to see that json over here. How do? 


    }else{ 
     console.log("another thing was searched"); 
    } 
/* 

    res.render('search-results', { 
     result: 'you gave me a url', 
     title: 'you gave me a url' 
    }); 
*/ 
}; 

我試着這樣做

var http = require('http'); 
var Shred = require("shred"); 
var assert = require("assert"); 

exports.query = function(req, res){ 
    //thequery is the string that is requested 
    var thequery = req.query.query; 
    var shred = new Shred(); 
    //I created a variable outside of the object 
    var myjson; 


    console.log("user searched" + " " + thequery); 
    console.log(); 

    //The if statement detects if the user searched a url or something else 
    if (thequery.indexOf("somearbitratyrestapi.com") !== -1){ 
     console.log("a url was searched"); 
     //find info on the url 

     var thedata = shred.get({ 
      url: "http://somearbitratyrestapi.com/bla/v2" + thequery, 
      headers: { 
      Accept: "application/json" 
      }, 
      on: { 
      // You can use response codes as events 
      200: function(response) { 
       // Shred will automatically JSON-decode response bodies that have a 
       // JSON Content-Type 

       //This is the returned json 
        //I set myjson to the returned json 
       myjson = response.content.body 

      }, 

      // Any other response means something's wrong 
      response: function(response) { 
      console.log("ohknowz"); 
      } 
      } 
     }); 

      //Then I try to output the json and get nothing 
      console.log(myjson); 


    }else{ 
     console.log("another thing was searched"); 
    } 
/* 

    res.render('search-results', { 
     result: 'you gave me a url', 
     title: 'you gave me a url' 
    }); 
*/ 
}; 

對不起,我的問題不好解釋。有人可以幫助或解釋發生了什麼。

回答

0

所以你認爲你需要將數據移出你的嵌套範圍,但事實恰恰相反。在這裏您可以訪問你的上游JSON響應嵌套的作用域,你需要訪問res對象,雖然它發送:

myjson = response.content.body 
res.send(myjson); 

然而,長期的,你需要做一些更多的節點教程和重點如何使用回調來避免深度嵌套的函數範圍。

+0

是的,我可能會切換到不同的休息api,如https://github.com/mikeal/request,因爲它處理範圍更好。但是你說的是我可以使用.send以某種方式逃避範圍,對吧? – aarocka 2013-02-14 12:52:36

+0

你說「逃避範圍」的方式好像你還沒有理解異步編程如何工作的「啊哈」時刻。這與節點的httpclient vs request與superagent或其他任何事情無關,這就是節點編程的過程。您可以使用函數參數,閉包,回調和流量控制範例(可能通過支持庫,如async.js或promises)。當IO完成時,它不會同步並阻塞,IO的結果顯示在同一範圍內。這完全是一個不同的範例。 – 2013-02-14 18:00:40