Please help me debug this hello world functions error. Logs in comment ->
Please help me debug this hello world functions error. Logs in comment ->
GET https://mydomain.com/api/hello 404
Response {type: 'basic', url: 'https://mydomain.com/api/hello', redirected: false, status: 404, ok: false, …}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
return new Response('Hello worker!');
}
async function fetchMovies() {
const response = await fetch('/api/hello');
// waits until the request completes...
console.log(response);
}
fetchMovies();
onRequestGet to handle requests: https://developers.cloudflare.com/pages/platform/functions/addEventListener('fetch', event => { syntax looks like a normal service worker Worker format - perhaps you're not using Functions and this is just a worker?// 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()onRequestGetaddEventListener('fetch', event => {// 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.stringifybodyJSON.stringify(body){key: value}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()