Created
May 29, 2021 07:01
-
-
Save lbr77/c724541c644915e83fe48e088b4ea761 to your computer and use it in GitHub Desktop.
基于CF-Workers的站点PV统计
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener("fetch", (event) => { | |
event.respondWith( | |
handleRequest(event) | |
); | |
}); | |
async function handleRequest(event){ | |
const url = new URL(event.request.url); | |
const method = event.request.method; | |
if(method === "GET"){ | |
let record = await JSON.parse(await KV.get("RECORD")) | |
let page = url.searchParams.get("page"); | |
// console.log(page) | |
if(page){ | |
let flag = 1; | |
for(let i = 0;i<record.length;i++){ | |
if(record[i].path===page){ | |
flag = 0; | |
record[i].time+=1; | |
} | |
} | |
if(flag==1){ | |
record.push({"path": page,"time": 1}); | |
} | |
event.waitUntil(KV.put("RECORD",JSON.stringify(record))); | |
} | |
return new Response(JSON.stringify(record),{status: 200}) | |
}else{ | |
return new Response("Method not allowed",{status: 405}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment