如何使用Node.js显示从http请求到外部API的图像

How to display image from http request to external API with Node.js

本文关键字:外部 API 图像 请求 http Node 何使用 js 显示      更新时间:2023-11-04

我遇到这样的情况,为了获得我正在构建的网站的图像,我需要向外部服务器发出http请求以获取信息。目前,来自请求的响应有两种形式,XML和图像。我使用Node.js.来完成这项工作

对于XML,我能够毫无问题地解析它,并且可以将它传递到一个变量中,并像处理其他一切一样进行处理。对于这些图像,我陷入了困境,我不知道如何在提出请求后将它们"显示"在页面上。我所能做的最远的事情就是在邮递员那里正确设置请求。我的问题是,我可以从我向另一台服务器发出的请求的响应中提取图像,并将其显示在我正在构建的web应用程序中吗?

我对后端世界很陌生,正在努力边学习边学习。这是一个我能够找到并用于解析从API获得的XML响应的示例

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
                });
            }
        );
    }
);
});

任何帮助都将不胜感激。谢谢

步骤1:获取图像并将其保存在节点服务器上。请求流媒体模块文档以获得更多选项

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

步骤2:发送保存的图像作为响应。

app.get('/display', function(req, res)) {
  fs.readFile('doodle.png', function(err, data) {
    if (err) throw err; // Fail if the file can't be read.
    else {
      res.writeHead(200, {'Content-Type': 'image/jpeg'});
      res.end(data); // Send the file data to the browser.
    }
  });
};