Return JSON
Return JSON directly from a Worker script, useful for building APIs and middleware.
addEventListener('fetch', event => {
const data = {
hello: 'world',
};
const json = JSON.stringify(data, null, 2);
return event.respondWith(
new Response(json, {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
})
);
});