简体   繁体   English

Javascript/node.js 在服务器和客户端之间发送数据

[英]Javascript/node.js sending data between server and client

I am writing a recipe search engine and i am stuck at sending data between client and server as i didn't work 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 (eg 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, how do i do that?然后在服务器上我想将类别传递给 API 调用,进行调用并将数据发送回客户端,我该怎么做? 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 doesn't logs or gives me anything so i don't think it even works app.get 没有记录或给我任何东西,所以我认为它甚至不起作用

try this:尝试这个:

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`
    fetch(url).then(res => res.json()).then(res => {
        response.json(json);
    });
})

I think that you should use only one 'post' method for that(on both frontend and backend).我认为您应该只使用一种“发布”方法(在前端和后端)。 Assuming that your code is correct, it should be something like that:假设您的代码是正确的,它应该是这样的:

app.post('/data', async (request, response) => {
    const data = request.body;
    const category = data.categoryChoice;
    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);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM