首页 > 解决方案 > 检查响应后,发送第二个响应

问题描述

当我收到服务器对一个字符串的响应时,我想发送下一个 json 对象。我想检查第一个对象并相应地发送第二个对象。怎样才能在服务器上响应?

app.post('/demo', (request, response) => {
    const data = {"name": "john", "surname": "doe"};
    const check = "Schedulable";
    
    if(check == "Schedulable"){
      response.send(check);       //I need to know if it is schedulable on client-side. Because i will use this information on client-side. How can i send string information. After that i want to send json object.
      response.json(data);
    }
    else{
      response.send("Not Schedulable");
    }    
    
});
Client-side Code:
{
    const response = await fetch('/demo', options);
    const info = await response.json();
    
    //Normally i do this when i receive a single response. How can i do when i have double response.
}

标签: javascriptnode.jsexpress

解决方案


您可以发送一个包含两个变量的对象,而不是将它们作为您无法执行的单独部分发送。例如:

{ check: check, data: data }

或者你甚至可以使用速记然后去:

{ check, data }

我希望这有帮助。我自己对此很陌生。


推荐阅读