I'm currently exploring the idea of incorporating multiple app.post functions in my project. Specifically, I have a client-side JavaScript function that sends a request to the server-side JavaScript to add content to a database using the app.post function. However, if I want to delete something from the database, the client needs to send a request along with the ID of the object to be deleted. The challenge is that my initial app.post function is only set up for adding items to the database.
Here's a snippet of the server-side JavaScript code:
const express = require("express");
const { request, response } = require("express");
const app = express();
app.listen(3000, () => console.log("listening at 3000"));
app.use(express.static("public"));
app.use(express.json({ limit: "1mb" }));
const database = new Datastore("database.db");
database.loadDatabase();
app.get("/api", (request, response) => {
database.find({}, (err, data) => {
if (err) {
console.log("An error has occurred");
response.end();
return;
}
response.json(data);
});
});
app.post("/api", (request, response) => { //Adding Content to the db
console.log("Server got a request!");
const data = request.body;
database.insert(data);
response.json(data);
});
And here's a glimpse of the client-side JavaScript code:
const data = { Item, ID };
const options = {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-type": "application/json",
},
};
fetch("/api", options);
My question revolves around finding a way to instruct the Server about which object to delete. While I know how to remove content from the database, I'm unsure about how to communicate this instruction effectively from the client to the server.