0

I was learning about removing a collection from MongoDB with express. I know I have to use app.delete when deleting something, but the way I see my code, it's actually Todos, or MongoDB that does deleting, by its deleteOne function that does anything relating to removing the content. So I guess this is not a question about how to do, but rather I want to understand how its actually work. Does the same question apply to app.Method(), like post for example. Can get replace delete in certain situations, or what is the benefit of specifying the method.

I tried changing everything to "get", that means app.delete -> app.get, and $.ajax type to get also, and it works perfectly fine.

This is the js file:

$(document).ready( () => {
    $('.delete-todo').on('click', (e) => {
        $target = $(e.target);
        const id = $target.attr('data-id');
        $.ajax({
            type: 'DELETE',
            url: '/todo/delete'+ id,

This is app.js

app.delete('/todo/delete:id', (req, res, next) => {
const query = {_id: ObjectID(req.params.id)}
Todos.deleteOne(query, (err, response) => {
Free Me
  • 195
  • 2
  • 10
  • 2
    Possible duplicate of [Which HTTP methods match up to which CRUD methods?](https://stackoverflow.com/questions/6203231/which-http-methods-match-up-to-which-crud-methods) – Dan O Sep 03 '19 at 18:53
  • Yeah I read through the post and it said exactly what i was trying to avoid. I'm not asking what delete does, it's literally in the name. I want to know what actually does the "Delete" part. In my example it was Mongo Driver with removeOne() function, in other cases it could be remove()... When I replace delete with get i also receive the exact result with no error. So is there a benefit for specifying the method? – Free Me Sep 04 '19 at 04:13
  • what specifically do you think the express.js `delete` method does? – Dan O Sep 04 '19 at 20:51

1 Answers1

0

The different HTTP methods could be used like:

app.delete('/todo:id', // Deletes the ToDO
app.get('/todo:id',    // Returns the TODO
app.post('/todo:id',   // Adds a TODO
app.patch('/todo:id',  // Edits a TODO
PLASMA chicken
  • 2,777
  • 2
  • 15
  • 25
  • Yes, i know what delete does, as i stated in my question. What I want to ask is which part really does that delete part. If i channge my code from delete to get, when I press delete, Mongo Driver will removeOne(), the server get reloaded with a get request, and the new web page will show that it has been deleted. Is there any wrong with that logic, because i did that, and it app works exactly the same as if it was a delete – Free Me Sep 04 '19 at 04:09
  • Nope, its just that you could overload paths with that. – PLASMA chicken Sep 04 '19 at 16:30