Last active
November 27, 2024 11:59
-
-
Save tarasowski/85663b7007f3134a099b42cf3d5c1ada to your computer and use it in GitHub Desktop.
azure functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app } = require('@azure/functions'); | |
app.http('list-todos', { | |
methods: ['GET', 'POST'], | |
authLevel: 'anonymous', | |
handler: async (request, context) => { | |
context.log(`Http function processed request for url "${request.url}"`); | |
const name = request.query.get('name') || await request.text() || 'world'; | |
return { body: `Hello, ${name}!` }; | |
} | |
}); | |
// eine Todo app braucht folgende Endpoints: | |
// GET /todos -> list all todos | |
// POST /todos -> create a new todo | |
// GET /todos/:id -> get a todo | |
// PUT /todos/:id -> update a todo | |
// DELETE /todos/:id -> delete a todo | |
let todos = [ | |
{ id: 1, text: 'Buy milk', done: false }, | |
{ id: 2, text: 'Walk the dog', done: true }, | |
] | |
app.http('todos', { | |
methods: ['GET', 'POST'], | |
authLevel: 'anonymous', | |
route: 'todos', // << immmer explizit angebenk | |
handler: async (request, context) => { | |
console.log(request.method); | |
if (request.method === 'POST') { | |
const todo = JSON.parse(await request.text()); | |
todo.id = todos.length + 1; | |
todos.push(todo); | |
return { body: JSON.stringify(todos) }; | |
} else { | |
return { body: JSON.stringify(todos) }; | |
} | |
} | |
}); | |
app.http('todo-by-id', { | |
methods: ['GET'], | |
authLevel: 'anonymous', | |
route: 'todos/{id}', | |
handler: async (request, context) => { | |
const id = parseInt(request.params.id, 10); | |
const todo = todos.find(t => t.id === id); | |
if (todo) { | |
return { status: 200, body: JSON.stringify(todo) }; | |
} else { | |
return { status: 404, body: 'Todo not found' }; | |
} | |
} | |
}); | |
// GET /todos/:id -> get a todo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment