Implementation of basic URL shortener.
Search Long URL from short URL
nanoid
utility used for generating URL friendly unique identifier for the the short URLs.- Returning existing short URL if a record for the corresponding long URL exist in the database.
mongodb
used as the database for persisting records.mongoose
used for interacting with Mongodb.
Note: Run backend first
Configs: Database connection settings needs to be upated in backend/.env
file with the following keys
Sample values shown below.
DB_HOST=cluster0-shard1.mongodb.net:27017,cluster0-shard2.mongodb.net:27017,cluster0-shard3.mongodb.net:27017
DB_USER=abc
DB_PASS=abc
DB_COLLECTION=url_shortener
KEY_LENGTH=6
MAX_COLLISION_COUNT=2
KEY_LENGTH : Length of the key to be used as unique identifier
MAX_COLLISION_COUNT : Maximum number of retries in case of collisions with the same key length. If the number of collisions increase beyond this count, the KEY_LENGTH
is increased by 1
temporarily to generate a longer unique key.
cd backend
npm install
npm start
- Default port for backend is
8000
, which can again be overridden inbackend/.env
file.
- Basic interface to generate shortened URL links using a custom domain.
- Option to copy the generated short URL to the clipboard.
- Also included an interface to search for the long URL from a given short URL.
Configs: Server host
and port
settings are present in the .env
file in the root folder.
Update the .env
as per the settings of the backend server.
Sample values shown below.
REACT_APP_SERVER_HOST=http://localhost
REACT_APP_SERVER_PORT=8000
- In the root folder -
npm install
npm start
- URL's not tied to a specific user or domain. Therefore, not shortening the same long URL again. If a record for a particular long URL exists, the corresponding short URL record is returned irrespective of the domain.
KEY_LENGTH
character short ID is generated usingnanoid
library. It uses 64 characters from the[A-Za-z0-9_-]
character set to generate the identifier.- Initially I'm using 6 character long keys. 6 character identifier using 64 chars gives approximately 64^6 ( ~68 Billion ) unique identifiers, which should be sufficient for this use case. If collisions occur, the length of the KEY to be generated is increased dynamically if the collision threshold is crossed. The
KEY_LENGTH
andMAX_COLLISION_COUNT
variables can be updated in thebackend/.env
file to increase the default length of the key.