0

こんにちは、Nodejs で REST API をセットアップしましたが、メソッド内から外部 REST サービスを呼び出そうとするまで、ajax 呼び出しは完全に機能しています。

なぜこれがうまくいかないのか、誰かが私を助けてくれますか? ポート3000で実行されているExpressでNodejsを使用しています。基本的にはlocalhostです。私の作業は進行中ですが、かなり変更されています。request モジュールを使用していましたが、http に戻りました。

app.get('/api/exchange', function (req, res){
    var text = '';
    var options = {
        host : 'rate-exchange.appspot.com',
        port : 3000,
        path : '/currency?from=GBP&to=USD',
        method : 'GET'
    };

    var call = http.request(options, function(res) {
        console.log("statusCode: ", res.statusCode);
        text = "made it in";

        res.on('data', function(d) {
            text = "here";
        });
    });

    call.end();
    call.on('error', function(e) {
        text = "error";
    });

    res.json({ msg: text });
});

すみません、classic javascript debuging technique基本的textにはどこに行くのかを確認するために使用しています。

これを実行すると、次のように返されます。

{
    "msg": ""
}

psノード用の優れたデバッグツールを知っている人は誰でもいいです。

4

1 に答える 1

2

port:3000は間違っていると思います。そして、コールバック関数から応答を取得する必要がありhttp.requestます。

app.get('/', function (req, res){
    var text = '';
    var options = {
        host : 'rate-exchange.appspot.com',
        //port : 3000,
        path : '/currency?from=GBP&to=USD',
        method : 'GET'
    };

    var call = http.request(options, function(resp) {
        console.log("statusCode: ", res.statusCode);
        text = "made it in";

        resp.on('data', function(d) {
            text = "here";
            console.log("response data: "+d);
        });
    });

    call.end();
    call.on('error', function(e) {
        text = "error";
    });

    //res.json({ msg: text });
});
于 2013-08-26T03:03:50.553 に答える