简体   繁体   English

Node.js lambda函数使重复的http调用

[英]Node.js lambda function makes repeated http calls

const https = require('https')

exports.handler = async(event) => {

    return new Promise((resolve, reject) => {

        const parsedEvent = event

        const data = JSON.stringify(parsedEvent.data)

        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };

        const endpoint = process.env.API_URL + '/' + parsedEvent.service

        const req = https.request(endpoint, options, (res) => {
            console.log('statusCode:', res.statusCode);
            console.log('headers:', res.headers);

            res.on('finish', () => {
                return resolve({
                    statusCode: res.statusCode
                })
            })
        });

        req.on('error', (e) => {
            return reject(e)
        });

        req.write(data)
        req.end()
    })
};

That's my lambda function and when I test it with the following data : 那就是我的lambda函数,当我用以下data测试它时:

{
  "service": "transcription/check",
  "data": {
    "ConversationId": 1
  }
}

it makes an HTTP call as expected, except it repeatedly makes HTTP calls until it times out. 它会按预期进行HTTP调用,除了它会反复进行HTTP调用直到超时为止。 Somehow, the lambda function doesn't know to end. 不知何故,lambda函数不知道会结束。 What am I doing wrong? 我究竟做错了什么?

Turns out I need to handle the data event. 原来我需要处理data事件。

Adding: 新增:

res.on('data', () => {return})

helped 帮助了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM