AWS Lambda-获取网站的HTML

时间:2017-01-27 01:35:01

标签: node.js amazon-web-services aws-lambda

我正在尝试在AWS Lambda中创建函数,该函数返回给定网站的html。那是我的代码:

console.log('Loading function');

exports.handler = (event, context, callback) => {

var util = require("util"),
http = require("http");

var options = {
    host: "www.nyuad.nyu.edu/en/news-events/abu-dhabi-events.html",
    port: 80,
    path: "/"
};

var content = "";   

var req = http.request(options, function(res) {
    res.setEncoding("utf8");
    res.on("data", function (chunk) {
        content += chunk;
    });

    res.on("end", function () {
        util.log(content);
        callback(null, content);
    });
});

req.end();
};

它非常适合'www.google.com'作为选项中的主机参数,但是当我尝试更复杂时,类似于代码中的给定参数,我收到一个错误:

Error: getaddrinfo ENOTFOUND www.nyuad.nyu.edu/en/news-events/abu-dhabi-events.html www.nyuad.nyu.edu/en/news-events/abu-dhabi-events.html:80

at errnoException (dns.js:26:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)

1 个答案:

答案 0 :(得分:1)

如果查看http module的文档,您会看到:

  • host:发出请求的服务器的域名或IP地址
  • 路径:请求路径,默认为'/'。应包括查询字符串(如果有)。例如。 '/index.html?page=12'