Deploy JavaScript functions as instant web services
async (request, env, ctx) => {
const url = new URL(request.url);
if (url.pathname === '/api/hello') {
return new Response(JSON.stringify({
message: 'Hello World!',
timestamp: new Date().toISOString()
}), {
headers: { 'Content-Type': 'application/json' }
});
}
return new Response(`
<h1>My API</h1>
<p>Try <a href="/api/hello">/api/hello</a></p>
`, {
headers: { 'Content-Type': 'text/html' }
});
}