EN
Express.js - AJAX DELETE request
0
points
In this article, we would like to show you how to make AJAX DELETE requests in Express.js.
1. DELETE method example
In this section, we use Express.js to handle DELETE requests.
index.js
const express = require('express'); // installstion: npm install express
const port = 3000;
const app = express();
app.delete('/examples/:id', (request, response) => {
const idToDelete = request.params.id;
// delete resource with ID: idToDelete
response.send('Example removed');
});
app.listen(port, () => {
console.log(`server is listening at http://localhost:${port}`);
});
To run the server use:
node index.js
Output:
server is listening at http://localhost:3000
2. Pure JavaScript (Vanilla JS) AJAX DELETE request
The following is an example execution of a fetch
method that sends DELETE requests.
<!DOCTYPE html>
<html>
<body>
<script>
fetch('/examples/5', {
method: 'DELETE'
})
.then(res => response.text())
.then(res => console.log(res))
.catch(error => {
console.error('Error:', error);
});
</script>
</body>
</html>