ShortLink V1.0.0 is a URL shortening service that allows you to create short, easy-to-remember links for any website. It is built with Node.js, TypeScript, Express.js, and MongoDB.
Name | Version |
---|---|
Node.js | v18.16.0 |
TypeScript | v5.0.4 |
Express.js | v4.18.2 |
Mongoose/MongoDB | v7.0.3 |
Jest | v29.5.0 |
The ShortLink
project has the following directory structure:
short-link/
├── src/
│ ├── controllers/
| | ├── authentication.controller.ts
| | ├── statistic.controller.ts
| | ├── shortlink.controller.ts
| | └── user.controller.ts
│ ├── helpers/
| | ├── auth.helper.ts
| | ├── shortlink.helper.ts
| | └── index.ts
│ ├── middlewares/
| | └── index.ts
│ ├── models/
| | ├── statistic.model.ts
| | ├── shortlink.model.ts
| | └── user.model.ts
│ ├── services/
| | ├── statistic.service.ts
| | ├── shortlink.service.ts
| | ├── user.service.ts
| | └── index.ts
│ ├── routes/
│ │ ├── v1/
| | | ├── authentication.route.ts
| | | ├── statistic.route.ts
| | | ├── shortlink.route.ts
| | | └── user.route.ts
| | └── index.ts
│ ├── tests/
| | ├── authentication.test.ts
| | ├── decode.shortlink.test.ts
| | ├── encode.shortlink.route.ts
| | └── statistic.shortlink.route.ts
│ └── index.ts
├── .env.example
├── package.json
├── babel.config.js
├── jest.config.js
├── nodemon.json
└── package.lock.json
├── package.json
├── postman_collection.json
├── README.md
└── tsconfig.json
src/controllers
: Contains the request handlers for each API endpointsrc/helpers
: Contains helper functions used throughout the projectsrc/middlewares
: Contains middleware functions used to handle incoming requestssrc/models
: Contains the Mongoose models for the MongoDB databasesrc/services
: Contains the business logic of the applicationsrc/routes/v1
: Contains the API routes for version 1 of the applicationsrc/routes/index.ts
: Combines all the routes into one routersrc/tests
: Contains the Jest tests for the applicationsrc/index.ts
: The entry point of the applicationpostman_collection.json
: Contains a collection of HTTP requests I used to test the API endpoints of the project.
ShortLink provides the following API endpoints:
End Point | Payload Sample | Function |
---|---|---|
POST /api/v1/auth/register |
{ username: "malish", email: "malish@indicina.co", password: "12345" } | Registers a new user with the application |
POST /api/v1/auth/login |
{ email: "malish@indicina.com", password: "12345" } | Logs in a user and setup cookie |
POST /api/v1/encode |
{ longURL: "https://indicina.co" } | Encodes an original URL into a short link |
GET /api/v1/decode |
{ shortURL: "http://short.est/" } | Decodes a short link into an original URL |
GET /api/v1/statistic/:url_path |
Gets the statistics for a particular short link |
To get started with ShortLink, follow these steps:
To set up a MongoDB cluster for ShortLink, follow these steps:
- First, create a
MongoDB account or login
. - Then create a new project. Give it a name and press the Next button.
- Then click Create Project after that.
- Create a database in the next window by selecting a cloud provider, a location, and specs. So press
Build a Database
to get going. - Choose "Shared" because it is sufficient for testing purposes. And then click Create.
- Next, select
aws
as your cloud provider and the region that is closest to you. Following your selection, clickCreate Cluster
.
The cluster's formation will take some time. Create a user to access your database in the meanwhile.
- Choose "My Local Environment" because you are runing the application locally. You can then add an IP addresses. To conclude,
click Close
. You will receive a URI string after the database is set up, which you will use to connect to the database. The string appears as follows:
mongodb+srv://<YOUR_USERNAME>:<YOUR_PASSWORD>@<YOUR_CLUSTER_URL>/<DATABASE_NAME>?retryWrites=true&w=majority
Replace the place holders with the actual credentials and put the string on line 5 of .env file.
You can install mongoDB locally using this guide
or use the default one from the .env.example file.
- Clone this repository to your local machine using git clone https://github.com/your-username/short-link.git.
- Install the required dependencies using
npm install
. - Create a .env file in the root of the project and fill in the required environment variables. See .env.example for an example configuration or rename the file to .env.
- Start the server using
npm run start
To run the tests for ShortLink, use the command npm run test
.
This will run all the Jest tests in the src/tests directory and give you a report of the test results.
The project main objective is to make three API endpoints available:
/api/v1/encode
: Encodes an original URL into a short link/api/v1/decode
: Decodes a short link into an original URL/api/v1/statistic/:url_path
: Gets the statistics for a particular short URL More end points will be aviable in V1.5.0 after my assessment with Indicina's HR.
Contributions are always welcome! If you'd like to contribute to this project, please create a fork of this repository and submit a pull request with your changes.
If you have any questions or feedback about ShortLink, please feel free to contact me at my email address: malishben360@gmail.com.