I have an HTML form which hits this endpoint. It sends data, but I don't want the page to refresh once sending the data. is this possible?
im not sure what the best solution is, there is no preventdefault in nodejs, but I don't want to redirect/refresh the same page
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static("public"));
const PORT = process.env.PORT || 5001;
app.get("/", (req, res) => {
  res.sendFile("index.html", { root: __dirname });
});
app.post("/rumi", (req, res) => {
  console.log(res.req.body);
  res.json({ status: "ok" });
});
app.get("/rumi", (req, res) => {
  res.sendFile("index2.html", { root: __dirname });
});
app.listen(PORT, () => {
  console.log(`app running on port ${PORT}`);
});
Here is the html :
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport"
        content="initial-scale=1.0, user-scalable=no" />
  <meta charset="UTF-8" />
  <title>Drawing Tools</title>
  <style type="text/css">
    #map,
    html,
    body {
      padding: 0;
      margin: 0;
      height: 100%;
    }
    #panel {
      width: 200px;
      font-family: Arial, sans-serif;
      font-size: 13px;
      float: right;
      margin: 10px;
    }
    #color-palette {
      clear: both;
    }
    .color-button {
      width: 14px;
      height: 14px;
      font-size: 0;
      margin: 2px;
      float: left;
      cursor: pointer;
    }
    #delete-button {
      margin-top: 5px;
    }
  </style>
  <script src="/js/script.js"></script>
  <script>
    function submitRumiForm(e) {
      e.preventDefault();
      var formData = new FormData(document.getElementById("rumiForm"));
      var options = {
        body: formData,
        method: "POST"
      }
      fetch("/rumi", options).then(function(data) {
        console.log("post successful");
      }).catch(function(err) {
        console.log(err);
      })
    }
    var el = document.getElementById("stair-button")
    if (el) {
      el.addEventListener("click", submitRumiForm);
    }
  </script>
</head>
<body>
  <div id="panel">
    <div id="color-palette"></div>
    <div>
      <button id="delete-button">Delete Selected Shape</button>
      <button id="delete-all-button">Delete All Shapes</button>
    </div>
    <form id="rumiForm"
          action="/rumi"
          method="POST">
      <div>
        <h3>Elements</h3>
      </div>
      <button type="submit"
              name="stairs"
              value="clicked"
              id="stair-button"
              onclick="console.log('hi')">
        <img src="./images/stair.png" />
      </button>
      <button type="submit"
              name="ramp"
              value="clicked"
              id="ramp-button">
        <img src="./images/ramp.png" />
      </button>
      <button type="submit"
              name="exit"
              value="clicked"
              id="exit-button"><img src="./images/exit.png" /></button>
      <button type="submit"
              name="narrow"
              value="clicked"
              id="narrow-button">
        <img src="./images/narrow.png" />
      </button>
      <button type="submit"
              name="wide"
              value="clicked"
              id="wide-button"><img src="./images/wide.png" /></button>
      <button type="submit"
              name="construction"
              value="clicked"
              id="construction-button">
        <img src="./images/construction.png" />
      </button>
    </form>
  </div>
  <div id="map"></div>
</body>
</html>
I have an HTML form which hits this endpoint. It sends data, but I don't want the page to refresh once sending the data. is this possible?
im not sure what the best solution is, there is no preventdefault in nodejs, but I don't want to redirect/refresh the same page