Stock/routes/api/pages/[name].ts

43 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2023-07-23 00:32:54 +09:00
import { Handlers } from "$fresh/server.ts";
import { Status, STATUS_TEXT } from "https://deno.land/std@0.195.0/http/mod.ts";
import { fromFileUrl, join } from "$std/path/mod.ts";
export const handler: Handlers = {
async GET(req, ctx): Promise<Response> {
const headers = new Headers({
"content-type": "application/json"
});
const path = join(fromFileUrl(import.meta.url), "../../../../dist", `${ctx.params.name}.json`);
console.log("path : ",path)
let stat;
try {
stat = await Deno.stat(path);
}
catch(err){
if(err instanceof Deno.errors.NotFound){
return await ctx.renderNotFound();
}
else {
throw err;
}
}
const mtime = stat.mtime ?? new Date(0);
const body = await Deno.readTextFile(path);
headers.set("last-modified", mtime.toUTCString());
2023-10-26 22:09:20 +09:00
console.log(mtime);
// headers.set("cache-control", "max-age=600");
2023-07-23 00:32:54 +09:00
2023-10-26 22:09:20 +09:00
// const ifModifiedSinceValue = req.headers.get("if-modified-since");
// if ( ifModifiedSinceValue &&
// mtime.getTime() <= new Date(ifModifiedSinceValue).getTime()
// ){
// return new Response(null, {
// status: Status.NotModified,
// statusText: STATUS_TEXT[Status.NotModified]
// })
// }
2023-07-23 00:32:54 +09:00
return new Response(body, {headers});
},
};