URL 단축

청록비
2 read

축소할 URL을 입력하세요.

로직 설명
  1. URL을 입력 받기.
  2. 검증하기
  3. short-code 생성
  4. 로직 설명
    // DB 
    const urls = new Urls(c);
    
    // max-id 가져오기
    let max = await urls.selectMax('id');
    if (!max || max === 0) {
    	max = 0;
    }
    
    // id + 1
    const number = String(max + 1);
    
    // 62진수로 변환
    const shortCode = StringUtil.textToBase62(number);
    
    // 결과 반환
    const result = await urls.insert({ code: shortCode, url: url });
    return c.json({ success: true, result: `https://${c.env.HOST}/s/${result.code}` });