http api 형태로 하여 api gateway를 생성하였을 때,
https://api경로.execute-api.ap-northeast-2.amazonaws.com/default/api명?파라미터명=데이터
[index.js]
const http = require('https')
exports.handler = async (event) => {
return httprequest(event).then((data) => {
const response = {
param: event,
statusCode: 200,
body: JSON.stringify(data),
};
return response;
});
};
function httprequest(event) {
return new Promise((resolve, reject) => {
const options = {
host: '호스트명(url경로)',
path: encodeURI('/[중간경로]?파라미터명='+event.queryStringParameters.파라미터명),
port: 443,
method: 'GET'
};
const req = http.request(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
});
}
댓글 달기