This repository contains my first ever project built in NestJS, it was requested to me as a part of a Job Application, where a series of requirements were given to me and this app would need to follow them.
This app serves the purpose of shortening URLs, you send a URL to certain API and it returns a shortened URL. Building this project I learned a lot about NestJS and building APIs, such as writting controllers, service, modules and entity layers as well as connecting it to the database and manipulating the data.
In order to run this project in your local machine you need to follow these steps:
- Clone the repository
- Move to the cloned repo path
- Install all the dependencies
npm install
- Create a database in mongoDB atlas and get the cluster connection URL
- Go to cluster overview
- Click on the connect button
- Select Connect your application with drivers
- Get the URL and replace with your credentials
- Import the URL within the Mongoose module in
/src/app.module.ts
- Build the project
npm run build
- Run in development mode
npm run start:dev
In order to shorten an URL you need to call the following APIs:
Send the URL you want to shorten in the body of the request
Body of the request:
{
"url": "urlYouWantToShorten.com"
}
Response
{
"_id": "645342839e681055a51803a2",
"url": "urlYouWantToShorten.com",
"newUrl": "nsa34d.sh/aGJzO",
"__v": 0
}
API Controller
@Post('encode')
@HttpCode(HttpStatus.CREATED)
encode(@Body('url') url: string) {
return this.urlshorterService.encode(url);
}
Request Example:
GET urlshorter/decode/645342839e681055a51803a2
Response
{
"_id": "645342839e681055a51803a2",
"url": "urlYouWantToShorten.com",
"newUrl": "nsa34d.sh/aGJzO",
"__v": 0
}
API Controller
@Get('decode/:id')
@HttpCode(HttpStatus.FOUND)
decode(@Param('id') id: string) {
return this.urlshorterService.decode(id);
}
If you would like to contribute to the project, please feel free to open a pull request.
This project is licensed under the MIT License.