'Can not get post result from node.js

I'm trying to get result from node.js api however, whenever i deploy my website, it gets html result like below. enter image description here

but it works when it's on local. but not on the deployed website. so i tried to use axios post then it gets 404 error. and another api is work but when i write new api then it gets error.

this is my node.js

//this post work just fine . 
app.post("/insertCodeVisual", async (req, res) => {
  const { data } = await req.body;

  const visualData = new CodeVisualData({
    data: data,
  });

  try {
    visualData.save((err, info) => {
      res.send(info._id);
    });
    console.log("success");
  } catch (error) {
    console.log(error);
  }
});

//but this post is not working 
app.post("/api/database", async (req, res) => {
  const { host, user, password, port, table, database } = await req.body;

  var connection = mysql.createConnection({
    host: host,
    user: user,
    password: password,
    port: port,
    database: database,
  });
  try {
    connection.connect();
  } catch (error) {
    res.send([["ERROR_CODE"], [error.code]]);
  }

  const sql = `SELECT * FROM ${table}`;

  connection.query(sql, function (err, results) {
    if (err) {
      return res.send([
        ["ERROR"],
        [`code : ${err.code}`],
        [`errno : ${err.errno}`],
        [`sqlMessage : ${err.sqlMessage}`],
      ]);
    } else {
      const parse = papa.unparse(results, {
        quotes: false, //or array of booleans
        quoteChar: '"',
        escapeChar: '"',
        delimiter: ",",
        header: true,
        newline: "\r\n",
        skipEmptyLines: false, //other option is 'greedy', meaning skip delimiters, quotes, and whitespace.
        columns: null, //or array of strings
      });

      const unparse = papa.parse(parse);
      res.send(unparse.data);
    }
  });
});


const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, "dist")));
app.get("/*", (req, res) => {
  res.sendFile(path.join(__dirname, "dist", "index.html"));
});

React.js

this one is working absolutely

  const insertData = async () => {
    try {
      if (confirm("are u sure? ")) {
        axios
          .post(`${backend}/insertCodeVisual`, {
            data: {
              client: client,
              header: header,
            },
          })
          .then(res => {
            setJustSavedDataId(res.data);
          });
      } else {
        return;
      }
    } catch (error) {
      console.log(error);
    }
  };

this below is not working when i deployed .

 const getDatabase = async () => {
    const url = `${backend}/api/database`;
    const api = await axios.post(url, db[id]);
    const data = api.data;

    try {
      setInfo({ ...info, [id]: data });
    } catch (error) {
      console.log(error);
    }
  };

So i wonder what cases make this kind of issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source