export default {
async fetch(request, env, ctx) {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET, POST',
};
const { pathname } = new URL(request.url);
try {
if (pathname === "/api/view/customers_data") {
const results = await env.DB.prepare(
"SELECT * FROM customers_data "
).all();
return new Response(JSON.stringify(results), {
headers: corsHeaders,
});
}else if (pathname === "/api/add/customer") {
const body = await request.json();
const results = await env.DB.prepare(
"INSERT INTO customers_data (cabin, name, email, phone, gender) VALUES(?1, ?2, ?3, ?4, ?5)"
).bind(body.cabin, body.name, body.email, body.phone, body.gender).run();
return new Response(JSON.stringify(results), {
headers: corsHeaders,
}); }
} catch (e) {
return Response.json(e);
}
});
}
};
export default {
async fetch(request, env, ctx) {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET, POST',
};
const { pathname } = new URL(request.url);
try {
if (pathname === "/api/view/customers_data") {
const results = await env.DB.prepare(
"SELECT * FROM customers_data "
).all();
return new Response(JSON.stringify(results), {
headers: corsHeaders,
});
}else if (pathname === "/api/add/customer") {
const body = await request.json();
const results = await env.DB.prepare(
"INSERT INTO customers_data (cabin, name, email, phone, gender) VALUES(?1, ?2, ?3, ?4, ?5)"
).bind(body.cabin, body.name, body.email, body.phone, body.gender).run();
return new Response(JSON.stringify(results), {
headers: corsHeaders,
}); }
} catch (e) {
return Response.json(e);
}
});
}
};