Descrição curta das rotas disponíveis para consumir a API de tasks (assumindo servidor em http://localhost:3000).
-
POST /tasks
- Cria uma nova task.
- Body (application/json): 4 spaces { "title": "Título da tarefa", "description": "Descrição (opcional)" }
- Resposta: 201 Created com o objeto task criado.
-
GET /tasks
- Lista todas as tasks (ordenadas por id desc).
- Resposta: 200 OK com array de tasks.
-
PUT /tasks/:id
- Atualiza título e descrição de uma task.
- Body (application/json): 4 spaces { "title": "Novo título", "description": "Nova descrição" }
- Respostas:
- 200 OK com task atualizada
- 404 Not Found se id não existir
-
PATCH /tasks/:id/status
- Atualiza apenas o status (inteiro) da task.
- Body (application/json): 4 spaces { "status": 1 }
- Respostas:
- 200 OK com task atualizada
- 404 Not Found se id não existir
-
DELETE /tasks/:id
- Remove a task pelo id.
- Respostas:
- 200 OK se removida
- 404 Not Found se id não existir
4 spaces
curl -X POST http://localhost:3000/tasks -H "Content-Type: application/json" -d '{"title":"Comprar pão","description":"Ir à padaria"}'
4 spaces
curl http://localhost:3000/tasks
4 spaces
curl -X PUT http://localhost:3000/tasks/1 -H "Content-Type: application/json" -d '{"title":"Novo","description":"Atualizado"}'
4 spaces
curl -X PATCH http://localhost:3000/tasks/1/status -H "Content-Type: application/json" -d '{"status":1}'
4 spaces
curl -X DELETE http://localhost:3000/tasks/1
Exemplos usando fetch (JavaScript, browser ou Node com fetch disponível):
// criar
async function criarTask() {
const res = await fetch('http://localhost:3000/tasks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'Comprar pão', description: 'Ir à padaria' })
});
const data = await res.json();
console.log(data);
}
// listar
async function listarTasks() {
const res = await fetch('http://localhost:3000/tasks');
const tasks = await res.json();
console.log(tasks);
}
// atualizar (title + description)
async function atualizarTask(id) {
const res = await fetch(`http://localhost:3000/tasks/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'Novo título', description: 'Nova descrição' })
});
const data = await res.json();
console.log(data);
}
// atualizar status
async function atualizarStatus(id, status) {
const res = await fetch(`http://localhost:3000/tasks/${id}/status`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status })
});
const data = await res.json();
console.log(data);
}
// deletar (sem corpo)
async function deletarTask(id) {
const res = await fetch(`http://localhost:3000/tasks/${id}`, {
method: 'DELETE'
});
// pode ser só status; se o servidor retorna JSON, use res.json()
if (res.ok) console.log('Task removida');
else console.error('Erro ao remover', res.status);
}