Shurl Express is a web application that allows you to shorten URLs. It was developed as part of a code challenge in a recruitment process.
The Node.js version, as specified in the Dockerfile, is 22.x
, which was the latest stable version of Node.js as of the launch of this app.
Since this was a development challenge, tsx was used. Therefore, no building of the app is done on the container.
Para uma versão em português do README, acesse o README em português
Here are some requirements made by the recruiter, simplified:
- Use latest stable version of Node.js
- Use TypeScript
- Implement token-based authentication, with user registration and login
- Shorten URLs with a maximum length of 6 characters for the slug
- Track the number of times each shortened URL is accessed
- Implement logical deletion of URLs
- Registers must have a update timestamp
- Build endpoints to:
- Shorten URLs (token optional)
- Access shortened URLs
- List shortened URLs by user (token required)
- Update shortened URLs (token required)
- Delete shortened URLs (token required)
- Use Docker Compose to build and run the project
- Implement Swagger documentation
- Configure pre-commit hooks
git clone https://github.com/GuilhermeCAz/shurl_express.git
cd shurl_express
2. Create a .env
file according to .env.example
docker compose up --build --detach
The following command can be used instead:
npm run up
Server should now be accessible at http://localhost:3000
.
To stop the server, use the following command:
docker compose down
POST /register
: Register a new user.POST /login
: Log in and obtain a JWT.
POST /urls
: Shorten a URL. Requires validoriginalURL
in the request body.GET /urls
: List all URLs associated with the authenticated user.PATCH /urls/:slug
: Update the original URL for a specific slug. Requires validoriginalURL
in the request body.DELETE /urls/:slug
: Logically delete a URL by slug.GET /:slug
: Redirect to the original URL.
The following commands can be used to interact with the API. Alternatively, you can use the Swagger UI at http://localhost:3000/docs
.
curl -X 'POST' \
'http://localhost:3000/register' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"email": "user@example.com",
"password": "Password123"
}'
curl -X 'POST' \
'http://localhost:3000/login' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"email": "user@example.com",
"password": "Password123"
}'
After logging in, you can use the JWT you received in the Authorization header by adding the following to your request:
-H 'Authorization: Bearer ${JWT}'
This enables associating shortened URLs with the user. Therefore, you are authorized to list, edit and delete URLs shortened by you.
curl -X 'POST' \
'http://localhost:3000/urls' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${JWT}' \
-H 'Content-Type: application/json' \
-d '{
"originalURL": "https://example.com"
}'
curl -X 'GET' \
'http://localhost:3000/urls' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${JWT}'
curl -X 'PATCH' \
'http://localhost:3000/urls/${slug}' \
-H 'accept: application/json' \
-H 'Authorization: Bearer ${JWT}' \
-H 'Content-Type: application/json' \
-d '{
"originalURL": "https://example.com"
}'
curl -X 'DELETE' \
'http://localhost:3000/urls/${slug}' \
-H 'accept: */*' \
-H 'Authorization: Bearer ${JWT}'
curl -X 'GET' \
'http://localhost:3000/${slug}' \
-H 'accept: */*' \
Swagger UI try it out
button does not work for this request due to CORS policy. Use the browser instead.