0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Deno KVで短縮URL【Deno URL Shortener】

Last updated at Posted at 2023-05-02

See the Pen Untitled by John Doe (@04) on CodePen.

import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

const kv = await Deno.openKv()

serve(async (req: Request) => {
    const id = new URL(req.url).pathname.slice(1)
    if (req.method === 'GET') {
        const { value } = await kv.get([id])
        return value ? Response.redirect(value) : new Response('404 Not Found', { status: 404 })
    } else if (req.method === 'POST') {
        await kv.set([id], await req.text())
        return new Response('201 Created', { status: 201 })
    }
})

URLの更新手順

curl https://sht.deno.dev/abc -d https://deno.com/blog/kv

管理画面で確認できます

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?