Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created June 30, 2021 06:26
Show Gist options
  • Save mayankchoubey/31b5545092cc666ac6404bb1d0986096 to your computer and use it in GitHub Desktop.
Save mayankchoubey/31b5545092cc666ac6404bb1d0986096 to your computer and use it in GitHub Desktop.
URL shortener - router.ts
import {Status} from "https://deno.land/std/http/http_status.ts";
import {HTTP_METHOD_GET, HTTP_METHOD_POST, ROUTE_SHORTEN, QUERY_PARAM_TARGET} from "./constants.ts";
import {getTarget, addTarget} from "./controller.ts";
import {sendResponseCode} from "./utils.ts";
export async function handleRequest(req: Request, resp: any) {
const u=new URL(req.url);
const path=u.pathname, target=u.searchParams.get(QUERY_PARAM_TARGET);
switch(req.method) {
case HTTP_METHOD_GET: {
getTarget(resp, path);
break;
}
case HTTP_METHOD_POST: {
if(path !== ROUTE_SHORTEN)
return sendResponseCode(resp, Status.NotFound);
addTarget(resp, target);
break;
}
default: {
return sendResponseCode(resp, Status.MethodNotAllowed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment