I have an Express server with a route for deleting a (psychology) experiment. (I have split my routes into multiple files. This route is in admin.js and thus its full route path is /admin/experiments/:expId)
router.route('/experiments/:expId')
.delete(tokens.verifyToken, adminValidator.isAdmin, (req, res) => {
const experimentId = req.params.expId;
const db = req.db;
db.removeExperiment(experimentId);
renderExperiments(req, res, db);
})
function renderExperiments(req, res, db) {
let allExps = expStandard.getAllExperiments(db);
res.render('adminExperiments', {
allExps: allExps,
approved_flag: APPROVED_EXPERIMENT,
pending_flag: PENDING_EXPERIMENT
});
}
This route deletes the experiment from the database and then calls a method which renders the associated adminExperiments.ejs view. This is an EJS template that simply creates an HTML table. Each row of the table corresponds to an experiment. And in each row I have included a delete button. Each delete button is associated with an EventListener for when the button is clicked:
A row from the table showing information about a given experiment
Here is the code in the associated js script (adminExperiments.js) which is executed on the client side:
document.addEventListener('DOMContentLoaded', registerDeleteExpListeners);
function registerDeleteExpListeners() {
let deleteButtons = document.querySelectorAll('.btn_exp_delete');
for (b of deleteButtons) {
b.addEventListener("click", deleteExperiment);
}
}
async function deleteExperiment(event) {
// ???
}
Question: What do I use in the event listener deleteExperiment() to connect to my Express route?
I tried using Fetch with a method header of "delete" and sending it to the URL for the route (/admin/experiments/:expId). Although I can send a delete request to the route and it is received, it does not render adminExperiments.ejs. Instead, a response object is sent back with a status of 200. That's great but it's not what I want. I want a new page rendered with the updated list of experiments.