Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active November 27, 2024 11:59
Show Gist options
  • Save tarasowski/85663b7007f3134a099b42cf3d5c1ada to your computer and use it in GitHub Desktop.
Save tarasowski/85663b7007f3134a099b42cf3d5c1ada to your computer and use it in GitHub Desktop.
azure functions
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