Assign a HTTP response body as variable for my hapi.js handler

251 Views Asked by At

How do I assign a variable from the HTTP response body of an HTTP request? I tried doing it like this, but it's getting a 502 error.

const https = require('https')
const getDadJokes = async (request, h) => {
    const dadJokesApi = {
        hostname: 'icanhazdadjoke.com',
        method: 'GET',
        path: '/', 
        headers: {
            'Accept': 'text/plain',
        },
    }
    const dadJokes = https.request(dadJokesApi, (res) => {
        res.on('data', (body) => body);
    });
    dadJokes.end();
    const response = h.response(dadJokes);
    return response;
};
0

There are 0 best solutions below