Sending data between server and client in Node.js and Javascript

Hey guys !
I am writing a recipe search engine and i am stuck at sending data between client and server as i didn’t work much with backend before (i am a beginner in coding).

On the client side i ask user to choose category which he wants the recipe for (e.g chicken).
Then the choice is saved in variable and that is being send to the server. That’s all working right.
Then on the server i want to pass the category to API Call, make the Call and send the data back to the client so i can display recipes on the page, how do i do that ? Here’s some code:
CLIENT-SIDE:

function getRecipes(category){
    const categorySearch = category.alt;
    let data = {
        categoryChoice: categorySearch
    }
    let options = {
        method: 'POST',
        headers: {
            "Content-type": "application/json; charset=UTF-8"
        },
        body: JSON.stringify(data)
    }
    const promise = fetch('/data', options);
    promise.then(response => {
        if(!response.ok){
            console.error(response)
        } else {
            return response.json();
        }
    }).then(result => {
        console.log(result);
    })
}

SERVER-SIDE

app.post('/data', async (request, response) => {
    const data = await request.body;
    const gotData = data.categoryChoice;
    const category = gotData;
    console.log(category);
    response.json(category);
    return category
})

app.get('/data', async (request, response) => {
    const cat = await category;
    const url = `https://edamam-recipe-search.p.rapidapi.com/search?q=${cat}&from=0&to=100`
    const fetch_response = await fetch(url);
    const json = await fetch_response.json();
    response.json(json);
})

app.get part doesn’t logs or gives me anything so i don’t think it even works :frowning:
Thanks in advance
Pawel

1 Like

Your app.post should look something like this:

app.post('/data', async (request, response) => {
    const data = await request.body;
    const gotData = data.categoryChoice;
    const category = gotData;
    console.log(category);

    const url = `https://edamam-recipe-search.p.rapidapi.com/search?q=${category}&from=0&to=100`
    const fetch_response = await fetch(url);
    const json = await fetch_response.json();
    response.json(json);
})

Also why so many awaits?