有关范围,node.js和express的问题

时间:2013-02-14 02:47:06

标签: javascript node.js inheritance express scopes

我真的很想在各种语言中理解范围和其他类似的东西。现在我正在构建一个快速应用程序,它接受用户输入,然后查询任意api,然后将其提供给控制台。为了处理剩下的api,我正在使用shred。我知道我可以使用内置get请求的节点,但出于某种原因,我永远无法让它工作。用户向我的应用程序发出以下get请求,/ query?query =。这就是我现在拥有的。我无法真正描述我正在做的事情,所以请阅读代码注释。

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' 
    });
 */
};

对不起我的问题的错误解释。有人可以帮助或解释发生了什么。

1 个答案:

答案 0 :(得分:0)

所以你认为你需要将数据从嵌套范围中移出,但事实恰恰相反。在您可以访问上游JSON响应的嵌套范围内,您需要访问res对象并通过它发送:

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

但是,从长远来看,你需要做更多的节点教程,并专注于如何使用回调来避免深层嵌套的函数范围。