如何使用Node.js显示从http请求到外部API的图像
How to display image from http request to external API with Node.js
我遇到这样的情况,为了获得我正在构建的网站的图像,我需要向外部服务器发出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.
}
});
};
相关文章:
- 使用ajax长轮询从外部API更新页面上的响应
- Javascript 外部 API 调用,在 for 循环中使用变量 URL
- 如何从外部api获取数据
- 如何在AngularJS中正确使用HTTP.GET?具体而言,对于外部API调用
- 从开放的外部API-visual studio-AJAX检索值
- 如何使用Node.js显示从http请求到外部API的图像
- 使用 Node 应用程序 (KeystoneJS) 调用外部 API
- 如何将外部 API 中的其他数据添加到集合中的现有模型
- Meteor:从服务器调用相互依赖的外部 API 并将数据转储到 db
- 如何将特定信息从外部 API 获取到我的 ng-repeat
- 流星:使用不同的参数多次调用外部 API 并发布数据
- 如何将二进制文件从 Meteor 上传到外部 API
- 使用外部 API 的数据绑定基诺 UI 网格
- Google Apps 脚本从外部 API 获取响应
- 使用jQuery的外部API GET()请求
- 将ngCsv与外部API一起使用
- 使用外部API过滤Angular中的类型
- 使用Angular连接到外部API
- 来自外部API的React组件的绑定数据
- Meteor:来自外部API调用的数据未呈现