You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

24 lines
800 B

import { Middleware } from "https://deno.land/x/oak@v7.3.0/mod.ts";
import { ensureFile } from "https://deno.land/std/fs/mod.ts";
import format from "https://deno.land/x/date_fns@v2.15.0/format/index.js";
await ensureFile("./pageviews.json");
interface IPageViews {
[key: string]: number;
}
const pageViews: IPageViews = JSON.parse((await Deno.readTextFile("./pageviews.json")) || "{}");
const PageviewsMiddleware: Middleware = async function (_, next) {
try {
const date = format(new Date(), "yyyy-MM-dd", {});
pageViews[date] = pageViews[date] ?? 0;
pageViews[date] = pageViews[date] + 1;
await Deno.writeTextFile("./pageviews.json", JSON.stringify(pageViews, null, 2));
} catch (e) {
console.log(e.message);
}
await next();
};
export default PageviewsMiddleware;