LoginSignup
0
0

LambdaのGETリクエストとNode.js Expressでのレスポンス

Last updated at Posted at 2024-05-01

LambdaでのGETリクエストの自分用の覚書です。

LamdaでGETリクエストをすると、Node.jsで書かれたサーバーから「hello world」という文字を返すということをやります。


レスポンス側 (Node.js)

フレームワークはexpressを使用

index.js
const express = require('express');
const app = express();

app.get('/hello', function(req, res) {

    res.send('hello world')
    
    }
)

リクエスト側 (Python)

lambda_function.py
import urllib.request

def lambda_handler(event, context):
    hello = get_hello()
    print(hello)

def get_hello():
    request = urllib.request.Request(
        '<URL>',
        method='GET',
        headers={'User-Agent': 'Mozilla/5.0'}
    )
    response = urllib.request.urlopen(request, timeout=30)    
    data = response.read().decode('utf-8')
    
    return data
    

これで、Lambdaを実行すると

「hello world」という文字列を取得できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0