Created
          June 30, 2021 06:26 
        
      - 
      
- 
        Save mayankchoubey/31b5545092cc666ac6404bb1d0986096 to your computer and use it in GitHub Desktop. 
    URL shortener - router.ts
  
        
  
    
      This file contains hidden or 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 {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