ShortLink is a simple project that allows the shortening of long URLs into more manageable links. This API is built with JavaScript and provides endpoints for shortening URLs, retrieving original URLs from shortened links, statistics tracking and redirecting generated links to the originals.
- Shorten long URLs into shorter, more manageable links.
- Retrieve original URLs from shortened links.
- Track statistics such as the number of visits for each shortened link.
Node version >=12.0.0 and npm
or yarn
installed on your machine.
git clone https://github.com/tdogubo/short-link.git
cd short-link
Using Yarn
yarn install
Using npm
npm install
- Start the server
yarn start
-
The API will be available at http://localhost:3001 by default.
-
Use the following endpoints to interact with the API:
- POST /v1/urls/encode: Shorten a long URL. Provide the long URL in the request body.
- POST /v1/urls/decode: Retrieve the original URL associated with a shortened link.
- GET /v1/urls/:id/statics: Retrieve statistics for a shortened link, such as the number of visits.
- GET /:id : Redirects to the original link and increments the
visited
field of the url.
curl -H 'Content-Type: application/json' \
-d '{"url": "https://example.com/"}' \
-X POST \
http://localhost:3001/v1/urls/encode
Response
{
"data": {
"original": "http://localhost:3001/Dz1dmlFKgZ"
}
}
curl -H 'Content-Type: application/json' \
-d '{"url": "http://localhost:3001/Dz1dmlFKgZ"}' \
-X POST \
http://localhost:3001/v1/urls/decode
Response
{
"data": {
"original": "https://example.com/"
}
}
curl http://localhost:3001/v1/urls/Dz1dmlFKgZ/statics
Response
{
"data": {
"id": "Dz1dmlFKgZ",
"originalUrl": "https://example.com/",
"shortUrl": "http://localhost:3001/Dz1dmlFKgZ",
"visited": 0,
"createdAt": "2024-03-27T23:06:45.891Z"
}
}