2016-03-31 14 views
1

ビルドしているサイトの画像を取得するために、外部サーバーに情報を要求する必要があります。現在、リクエストからの応答は、XMLとイメージの2つの形式になっています。私はNode.jsを使ってこれをやっています。Node.jsを使用してhttpリクエストから外部APIに画像を表示する方法

XMLについては、問題なく解析でき、変数に渡して他のすべてと同じように処理できます。画像では、私は立ち往生している、私は彼らのための要求をした後、ページにそれらを "表示"する方法がわからない。私が得ることができる最も遠いのは、郵便配達員の要求を正しく設定することです。私の質問は、私が別のサーバーに行っているリクエストの応答の本文からイメージを引き出し、それをビルドしているWebアプリケーションに表示させることができますか?

私はバックエンドの世界では非常に新しく、私が行っているように勉強しようとしています。これは私が見つけて、私はすべてのヘルプは非常に高く評価されるだろう

var request = require("request"); 
var express = require("express"); 
var jsxml = require("node-jsxml"); 
var app = express(); 
var fs = require("fs"); 

app.get('/users', function(req,res) { 
console.log("List of users requested."); 
// We will grab the list of users from the specified site, but first we have to grab the site id 
// (Same idea as when we added users. We could have checked if req.session.SiteID has been populated, 
// but I chose to keep it simple instead) 
request(
    { 
     url: 'http://' + SERVERURL + '/api/2.0/sites/' + SITE + '?key=name', 
     headers: { 
      'Content-Type': 'text/xml', 
      'X-Tableau-Auth': req.session.authToken 
     } 
    }, 
    function(err, response, body) { 
     if(err) { 
      req.session.err = err; 
      res.redirect('/'); 
     } else { 
      var bodyXML = new jsxml.XML(body); 

      console.log("site id: " + siteID); 
     } 
     // OK. We have the site, now let's grab the list of users 
     // Since we're just making a GET request, we don't need to build the xml. All the is needed 
     // is the SiteID which is inserted in the url and the auth token which is included in the headers 
     request(
      { 
       url: 'http://' + SERVERURL + '/api/2.0/sites/' + siteID + '/users/', 
       headers: { 
        'Content-Type': 'text/xml', 
        'X-Tableau-Auth': authToken 
       } 
      }, 
      function(err, response, body) { 
       if(err) { 
        req.session.err = err; 
       } else { 
        // A succesful request returns xml with a <users> which contains multiple <user> elements. 
        // The <user> elements have name attributes and id attributes which we'll grab, store in a 
        // javascript object and render those in the html that loads. 
        var bodyXML = new jsxml.XML(body); 
        bodyXML.descendants('user').each(function(item, index) { 
         userIDs[item.attribute('name').getValue()] = item.attribute('id').getValue(); 
        }); 
        for(var user in userIDs) { 
         console.log(user + " " + userIDs[user]); 
        } 
       } 
       res.render("users.ejs", { 
        err: req.session.err, 
        userIDs: userIDs, 
        site: SITE 
       }); 
      } 
     ); 
    } 
); 
}); 

APIから取得することをXML応答を解析するために使用しないことができたものの一例です。ありがとう!

答えて

1

手順1:イメージをフェッチし、ノードサーバーに保存します。 request module documentation on streaming for more options

request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png')); 

ステップ2:保存されたイメージを応答として送信します。

+0

私は信じています。再び私はこれで新しいです。私はテキスト変数を知っている、私はそれらをクライアント側に通過するようにページを要求した後に定義することができます。この画像のリクエストはどのようにhtmlサイトのように見えますか?それは簡単なタグですか? –

+0

はい、それは単純なタグです。ソースページで画像を撮り、あなた自身を探すことができます。 –

関連する問題