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

**URL:** <https://forum.freecodecamp.org/t/sending-data-between-server-and-client-in-node-js-and-javascript/412476>\
**Category:** General\
**Created:** [July 29, 2020, 2:30pm UTC](https://forum.freecodecamp.org/t/sending-data-between-server-and-client-in-node-js-and-javascript/412476 "2020-07-29T14:30:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PawelKrzesinski](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pawelkrzesinski/32/137579_2.png) [@PawelKrzesinski](https://forum.freecodecamp.org/u/PawelKrzesinski)\
**Post date:** [July 29, 2020, 2:30pm UTC](https://forum.freecodecamp.org/t/sending-data-between-server-and-client-in-node-js-and-javascript/412476/1 "2020-07-29T14:30:42Z")

</div>

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:

```auto
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

```auto
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 ☹  
Thanks in advance  
Pawel

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [July 29, 2020, 2:38pm UTC](https://forum.freecodecamp.org/t/sending-data-between-server-and-client-in-node-js-and-javascript/412476/2 "2020-07-29T14:38:47Z")

</div>

Your `app.post` should look something like this:

```auto
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 `await`s?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:45pm UTC](https://forum.freecodecamp.org/t/sending-data-between-server-and-client-in-node-js-and-javascript/412476/3 "2021-06-01T18:45:16Z")

</div>


