Skip to content

Instantly share code, notes, and snippets.

@windchime-yk
Created December 17, 2022 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save windchime-yk/d22f95396f779de8bf86bf89ca2f3934 to your computer and use it in GitHub Desktop.
Save windchime-yk/d22f95396f779de8bf86bf89ca2f3934 to your computer and use it in GitHub Desktop.
Deno Advent Calendar 2022の17日目のサンプルコード
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