如何使用Node.js

时间:2016-03-31 15:37:35

标签: javascript xml node.js http tableau-server

我的情况是,为了获取我正在构建的网站的图像,我需要向外部服务器发出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 个答案:

答案 0 :(得分:2)

步骤1:获取图像并将其保存在节点服务器上。 request module documentation on streaming for more options

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