Fetch API snippet in a node.js Express app

1 min read

Summary

Below is a very simplified Fetch API POST request that is triggered on the click of a button.

 document
    .getElementById("buttonEventSend")
    .addEventListener("click", function () {
      //--
      fetch("http://localhost:3000/api/users", {
        method: "post",
        body: editor.getValue(),
        headers: { "Content-Type": "application/json" },
      })
        .then((res) => res.json())
        .then((json) => console.log(json))
        .catch((err) => console.log(err));
      //--
    });

The response is sent back as a JSON object.

You must be logged in to post a comment.