-
-
Save windchime-yk/d22f95396f779de8bf86bf89ca2f3934 to your computer and use it in GitHub Desktop.
Deno Advent Calendar 2022の17日目のサンプルコード
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
import { | |
type Handler, | |
serve, | |
} from "https://deno.land/std@0.168.0/http/server.ts"; | |
import { contentType } from "https://deno.land/std@0.168.0/media_types/mod.ts"; | |
// @deno-types="https://esm.sh/@planetscale/database@1.5.0/dist/index.d.ts" | |
import { connect } from "npm:@planetscale/database@1.4.0"; | |
import "https://deno.land/std@0.168.0/dotenv/load.ts"; | |
export const sampleApi = async () => { | |
const conn = connect({ | |
host: Deno.env.get("PS_HOST"), | |
username: Deno.env.get("PS_USERNAME"), | |
password: Deno.env.get("PS_PASSWORD"), | |
}); | |
const response = await conn.execute("SELECT * FROM tbl_test"); | |
return new Response(JSON.stringify(response.rows), { | |
headers: { | |
"Content-type": contentType("json"), | |
}, | |
}); | |
}; | |
const handler: Handler = (req) => { | |
const { pathname } = new URL(req.url); | |
// API | |
if (pathname.startsWith("/api")) return sampleApi(); | |
// 404画面 | |
return new Response("Not Found", { status: 404 }); | |
}; | |
const PORT = 9000; | |
serve(handler, { port: PORT }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment