Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fabianogoes/0d4495f4994cf34283f5ba7427c4decf to your computer and use it in GitHub Desktop.

Select an option

Save fabianogoes/0d4495f4994cf34283f5ba7427c4decf to your computer and use it in GitHub Desktop.
REST CRUD com NodeJS em 5 passos - Passo 5 - Delete

Passo 5 - Delete

Chegamos ao nosso ultimo método do CRUD que é DELETE, neste método assim como no GET por id, recebemos o id da Task que deve ser deletada pela Path:

app.delete('/tasks/:id', (req, res) => {
        console.log('delete...');
        let id = req.params.id;
        let index = tasks.findIndex((task => task.id === id));
        tasks.pop(index);
        res.json({});
});

Testando

curl -v -X DELETE http://localhost:3000/tasks/8dcea580-619b-4c99-83d8-56e6c457f67e

output

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> DELETE /tasks/8dcea580-619b-4c99-83d8-56e6c457f67e HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 2
< ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
< Date: Fri, 08 Sep 2017 02:59:07 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment