```js addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); }); a

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  if (request.method === 'POST') {
    // Parse form data from the request
    const formData = await request.formData();
    const user = formData.get('User');
    const quote = formData.get('Quote');
    const date = parseInt(formData.get('date'));
    const visibility = parseInt(formData.get('visibility'));

    // Send data to D1 database
    const response = await fetch('https://quote.logozip18.workers.dev/', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      
        'Access-Control-Allow-Origin': '*', 
        'Access-Control-Allow-Methods': 'POST',
        'Access-Control-Allow-Headers': 'Content-Type',
      },
      body: JSON.stringify({ user, quote, date, visibility }),
    });

    if (response.ok) {
      return new Response('Data submitted successfully!', { status: 200 });
    } else {
      return new Response('Error submitting data to database', { status: 500 });
    }
  } else {
    return new Response('Method not allowed', { status: 405 });
  }
}
Was this page helpful?