어느 서비스에서나 쓰일 수 있는 url 주소 단축 기능은, 주로 주소 복사 및 공유할 때 쓰인다. 불필요하게 긴 주소는 공유할 때 번거롭기 때문이다. 이번 프로젝트에서는 단순 기능개발에 목적을 두기보다, 구조화와 개발 환경 구축에 초점을 맞추었다.
- 구조화: 코드 중복을 최소화하기 위해 서비스 클래스를 구조화 / ‘NestJS Exception Filter’
- DB 만들기 = entity, table 등
- 초기세팅 (nestJs, typescript, typeorm, ormconfig.js) : nest cli
- short url 생성 api 만들기
-
original url, user ip 를 서버에 전송 (POST)
-
서버에서 short url 생성
-
localhost:3000/ 뒤에 위치하는 shorturl (const fullShortUrl = baseUrl + shortUrl; )
-
short url 고유하게 만드는 로직: (도메인 부분을 제외한) 뒷부분’ 절대 중복되지 않도록 (영문 대소문자, 가끔 하나의 숫자)
-
user ip로 하루 short URL 변환 횟수 확인: 저장한 ip 주소 - 일 30회만 변환 가능하게 작업
-
-
DB에 데이터 저장: original url , short url, user IP (insert into)
-
short url 반환 (return)
DB에서 원래 주소를 찾은 후 Redirect : 서버에서 - 뒷부분 조회 (select) : mapping 된 거를 redirect 시킴
- url의 parameter(short url)로 DB에서 original url 검색(조회 = select)
- original url로 Redirect
- ormconfig.js 파일 생성
- shorturl 칼럼에 unique 제약조건 추가
일반적으로 사용되는 https://www.shorturl.at/ , https://bitly.com/ 를 참고하여, 도메인 부분은 localhost:port로 default 값으로 하고, 뒷부분이 변경되도록
A progressive Node.js framework for building efficient and scalable server-side applications.
Nest framework TypeScript starter repository.
$ npm install
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
Nest is MIT licensed.