①ウェブページのjavascriptからhttpリクエストをAWSのlambdaに送って、
②lambdaから他社にhttpリクエストを送ってレスポンスを取得して、
③そのレスポンスを最初のウェブページに送り返す。
ということを実現したいです。
①からの引数の渡し方か、②のreturnかresの処理が間違っていると思うのですが、ご助言いただけると助かります。
①と③のjavascript
javascript
1fetch('https://amazon_lambda_xxxyyyzzz', { 2 method: 'post', 3 headers: { 4 'Content-Type': 'application/json' 5 }, 6 body: JSON.stringify({ 7 'id': "A1234" 8 }) 9 }) 10 .then(function (data) { 11 return data.json(); 12 }) 13 .then(function (json) { 14 document.getElementById('slotA').textContent = json + "点"; 15 });
②のlambda node.js
node.js
1const https = require('https'); 2 3exports.handler = function (event) { 4 const eb = JSON.parse(event.toString()); 5 const url = "https://abc.com/api/users/" + eb.id; 6 const req = https.request(url, (res) => { 7 res.on('data', (chunk) => { 8 const chunkString = chunk.toString(); 9 const obj = JSON.parse(chunkString); 10 console.log(obj.credit); 11 req.write(obj.credit); 12 }); 13 res.on('end', () => { 14 console.log('No more data in response.'); 15 }); 16 }) 17 18 req.on('error', (e) => { 19 console.error(`problem with request: ${e.message}`); 20 }); 21 22 req.end(); 23}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。