Cloudflare Functions, basically.
Cloudflare Functions, basically.
// Reacts to POST /api/hello
export async function onRequest(request) {
// ...
const body = { "products" : [
{ "Name": "Cheese", "Location": "Refrigerated foods"},
{ "Name": "Crisps", "Location": "the Snack isle"},
{ "Name": "Pizza", "Location": "Refrigerated foods"},
]}
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json',
};
return new Response(body, { headers });
};JSON.stringify the body, but otherwise yes JSON.stringify(body){key: value} structure is called in JS.async function fetchProductsAsJSON() {
const response = await fetch('https://mydomain.com/api/hello');
// waits until the request completes...
console.log(response);
}
fetchProductsAsJSON();const json = await response.json()mdn response json or mdn fetch will both have it
init on here https://developer.mozilla.org/en-US/docs/Web/API/fetch
context.env.VARIABLE_NAMEplatform is always undefined in the endpoints. How can I fix this so I can access platform.env while testing locally with wrangler2?platform is always undefined: https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare#environment-variablesnext() instead context.next() in your example// Reacts to POST /api/hello
export async function onRequest(request) {
// ...
const body = { "products" : [
{ "Name": "Cheese", "Location": "Refrigerated foods"},
{ "Name": "Crisps", "Location": "the Snack isle"},
{ "Name": "Pizza", "Location": "Refrigerated foods"},
]}
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json',
};
return new Response(body, { headers });
};async function fetchProductsAsJSON() {
const response = await fetch('https://mydomain.com/api/hello');
// waits until the request completes...
console.log(response);
}
fetchProductsAsJSON();fetch(url, {
headers: {
'foo': 'bar'
}
}