Complete the code to define an Edge Function that returns a simple message.
export async function [1](request) { return new Response('Hello from Edge Function!'); }
The Edge Function must export a fetch function to handle incoming requests.
Complete the code to read JSON data from the request body inside an Edge Function.
export async function fetch(request) {
const data = await request.[1]();
return new Response(JSON.stringify(data));
}text() returns raw text, not parsed JSON.To parse JSON data from the request body, use request.json().
Fix the error in the Edge Function that tries to access environment variables.
export async function fetch(request) {
const apiKey = [1].get('API_KEY');
return new Response(apiKey);
}window, process.env or global will fail.Environment variables are accessed via Deno.env.get('API_KEY') in Edge Functions.
Fill both blanks to create a response with JSON content and proper headers.
export async function fetch(request) {
const data = { message: 'Hello' };
return new Response(JSON.stringify(data), { [1]: { 'Content-Type': [2] } });
}body instead of headers in the options object.The response headers must include Content-Type: 'application/json' to indicate JSON data.
Fill all three blanks to handle a POST request and return a JSON response with status 201.
export async function fetch(request) {
if (request.method === [1]) {
const data = await request.[2]();
return new Response(JSON.stringify(data), { status: [3], headers: { 'Content-Type': 'application/json' } });
}
return new Response('Method Not Allowed', { status: 405 });
}To handle POST requests, check request.method === 'POST', parse JSON body with request.json(), and respond with status 201 for created.