从 flutter 到 node.js 服务器的 http 请求

问题描述 投票:0回答:0

我在 index.js 中写了这个:

const rules = [
  [['check engine light is on', 'car wont start'], 'Dead battery'],
  [['car wont start', 'clicking sound'], 'Bad starter motor'],
  [['car wont start', 'no sound'], 'Bad battery connection'],
  [['car stalls or dies while driving', 'car wont restart'], 'Faulty alternator']}


// Working memory and agenda
let working_memory = [];
let agenda = [];

// Inference engine
function backward_chaining(symptoms) {
  working_memory = working_memory.concat(symptoms);
  agenda = agenda.concat(symptoms);

  let diagnosis_made = false; // flag to check if a diagnosis has been made

  while (agenda.length) {
    const symptom = agenda.pop();

    for (const rule of rules) {
      if (rule[0].includes(symptom)) {
        const premise = rule[0].filter(s => s !== symptom);

        if (premise.every(p => working_memory.includes(p))) {
          working_memory.push(rule[1]);
          agenda.push(rule[1]);

          // If a rule was fired, a diagnosis has been made
          diagnosis_made = true;
        }
      }
    }
  }

  if (!diagnosis_made) { // if no diagnosis was made, return "No diagnosis found"
    return "No diagnosis found";
  }

  const diagnosis = working_memory[working_memory.length - 1];
  return diagnosis;
}


app.use(express.json());

// Endpoint
app.post('/infer', (req, res) => {
  const symptoms = req.body.symptoms;
  const diagnosis = backward_chaining(symptoms);
  res.json({ diagnosis: diagnosis });
});


const port = 3000;
app.listen(port, () => console.log(`Server is listening on port ${port}...`));

当我在 thunder 客户端中测试我的 api 时,它工作正常,但是当我通过 flutter 应用程序向它发送发布请求时,我总是得到 No diagnostic found,直到我测试它,所以如果我在 thunder 客户端中测试它然后立即从我的 flutter 页面发送相同的请求,我得到一个响应,然后如果我重新启动服务器并重新发送请求,我再次收到无诊断响应,所以看起来测试正在创建一些 flutter 用于响应的缓存,怎么能我解决这个问题直接得到响应而不需要测试它

我尝试添加超时但没有成功,(请注意,flutter 发送的 url 是托管服务器的机器 ip 地址,因为 localhost 在 flutter 中不起作用)

node.js flutter http post
© www.soinside.com 2019 - 2024. All rights reserved.