Node.js 12.x를 사용하는 AWS Lambda 함수가 있습니다. 내 코드는 다음과 같습니다.
exports.handler = async function(event, context) {
const https = require('https');
const sheetId = 01234;
const testData = JSON.stringify({"toTop":true, "cells": [ {"columnId": 3148210723153796, "value": 'TEST'} ] });
const options = {
hostname: 'api.website.com',
port: 443,
path: `/logs`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer oefohjeoiefoijn'
}
};
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.write(testData);
req.end();
}
요청이 실행되고 있지 않습니다. Cloudwatch에서 오류가 발생하지 않습니다. 처리기 내보내기가 제거 된 정확한 코드는 내 컴퓨터의 Node.js 12.x에서 제대로 작동합니다.
콜백이 반환 될 때까지 기다리지 않기 때문에 응답을 받기 전에 람다 함수가 종료됩니다.
요청을 약속으로 래핑 할 수 있습니다.
exports.handler = async function(event, context) {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
// ...
resolve();
});
req.on('error', (e) => {
reject();
});
req.write(testData);
req.end();
});
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다