Skip to content

Instantly share code, notes, and snippets.

@jonathanhudak
Created May 22, 2022 04:56
Show Gist options
  • Save jonathanhudak/d42239bfbf8e92fd66268e59d0060360 to your computer and use it in GitHub Desktop.
Save jonathanhudak/d42239bfbf8e92fd66268e59d0060360 to your computer and use it in GitHub Desktop.
Simple Deno Web Server for Generating QR Codes
import { qrcode } from "https://deno.land/x/qrcode/mod.ts";
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
// https://github.com/denorg/qrcode
// https://github.com/oakserver/oak
interface HTMLPageOptions {
body: string;
}
const makeHTMLPage = ({ body }: HTMLPageOptions) => `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>QR Code Generator</title>
</head>
<body>
${body}
</body>
</html>`;
const router = new Router();
router.get("/", async (context) => {
const params = new URLSearchParams(context.request.url.searchParams);
const qrCode = params.get("qrCode");
let qrCodeImage = "";
if (qrCode) {
const src = await qrcode(qrCode);
qrCodeImage = `<img src="${src}" alt="qr code" />`;
}
console.debug(qrCode);
context.response.body = makeHTMLPage({
body: `<form method="get" action="/">
<label for="qrCode">QR Code</label>
<input type="text" name="qrCode" id="qrCode" />
<button type="submit">Create QR Code</button>
<hr />
${qrCodeImage}
<form>`,
});
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8002 });
@jonathanhudak
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment