如何使用nodejs AWS Lambda发送http请求? [英] How to send http request with nodejs AWS Lambda?

查看:386
本文介绍了如何使用nodejs AWS Lambda发送http请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS Lambda来推动Alexa Skill Kit开发。在尝试跟踪事件时,我希望脚本在启动时发送HTTP请求,但是从云日志中看起来好像在执行过程中跳过了http.get函数。

I'm using AWS Lambda to drive an Alexa Skill Kit development. In an attempt to track events, I'd like the script to send an HTTP request on launch, however from the cloud logs it appears as though the http.get function is being skipped during the execution process.

代码如下所示(google.com取代了分析跟踪网址 - 已在浏览器中测试过);

The code is shown below (google.com replaces the analytics tracking url - which has been tested in the browser);

exports.handler = function (event, context) {

    var skill = new WiseGuySkill();
    var http = require('http');

    var url = 'http://www.google.com';
    console.log('start request to ' + url)
    http.get(url, function(res) {
        console.log("Got response: " + res.statusCode);
        // context.succeed();
    }).on('error', function(e) {
        console.log("Got error: " + e.message);
        // context.done(null, 'FAILURE');
    });
    console.log('end request to ' + url);

    skill.execute(event, context);
};

上下文对象已被注释掉以允许'skill.execute'运行,但无论哪种方式此HTTP请求未执行。只记录'start'和'end'console.logs,函数内部的那些没有。

The context objects have been commented out to allow for 'skill.execute' to function, yet either way this HTTP request is not executing. Only the 'start' and 'end' console.logs are recorded, those internal in the function do not.

这是一个异步问题吗?谢谢。

Is this a async issue? Thanks.

推荐答案

为了成功完成http请求,必须将http.get函数合并到回调函数中。否则,该过程将无法完成并将提前结束,使用回调允许http请求完成(有或没有错误),然后继续执行其余功能。

In order to successfully complete the http request, the http.get function must be incorporated into a callback function. Else the process will not be completed and will end prematurely, using a callback allows the http request to complete (with or without an error) before continuing with the rest of the function.

WiseGuySkill.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {

    // Call requestFunction to make the http.get call.
    // Get response from requestFunction using requestCallback
    requestFunction(function requestCallback(err) {

        // If error occurs during http.get request - respond with console.log
        if (err) {
            console.log('HTTP Error: request not sent');
        }

        ContinueIntent(session,response);
    });
};

函数'requestFunction'调用http.get并触发回调。

The function 'requestFunction' calls http.get and fires the callback.

function requestFunction(requestCallback){

        var url = "http://www.google.com";

        http.get(url, function(res) {
            console.log("Got response: " + res.statusCode);
            requestCallback(null);
        }).on('error', function (e) {
            console.log("Got error: ", e);
        });
    }

显然确保你在脚本开头需要'http'。
希望这可以帮助其他新人!

And obviously ensure you have required 'http' at the start of the script. Hope this helps anybody else new to this!

这篇关于如何使用nodejs AWS Lambda发送http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆