We have 2000000 records & store in json array then trying to post data into KV storage using worker.js, but not able to post data by calling post json API. Whenever posting large numbers of records showing CPU time limit issues & data not saved. Please check my below code & suggest me that where is wrong. Actually whenever run below api so not able to fetching body content, that’s why data not posting.
const corsHeaders = {
‘Access-Control-Allow-Origin’: ‘*’,
‘Access-Control-Allow-Methods’: ‘GET,HEAD,POST,OPTIONS’,
‘Access-Control-Max-Age’: ‘86400’,
};
async function gatherResponse(response) {
const { headers } = response;
const contentType = headers.get(‘content-type’) || ‘’;
if (contentType.includes(‘application/json’)) {
return JSON.stringify(await response.json());
} else if (contentType.includes(‘application/text’)) {
return response.text();
} else if (contentType.includes(‘text/html’)) {
return response.text();
} else {
return response.text();
}
}
async function handleRequest() {
const init = {
body: JSON.stringify(body),
method: ‘POST’,
headers: {
‘content-type’: ‘application/json;charset=UTF-8’,
},
};
const response = await fetch(url, init);
const results = await gatherResponse(response);
return new Response(results, init);
}
addEventListener(‘fetch’, event => {
return event.respondWith(handleRequest());
});
Please check my KV storage format is it correct or not