My small project to makes links short! 🌍
Making this project was a great opportunity to learn and test some new things! 🔥
Demo avialable soon! (Need to do frontend)
·
Report Bug
·
Request Feature
Table of Contents 📔
One of the most important things for everyone is to constantly keep on learning new things. That's why I've decided to create this project, since there weren't any MEVN (or MEN for now since Vue.js frontend will be added later) stack projects on my GitHub page. This project is quite simple. You give your very long URL to the API and it returns a shortened version you can use! I've also added users and admins that can perform more actions! I've also added statistics for each link which monitors how many times each link was clicked and from what region!
This project was a great opportunity to learn Node itself! It gave me a better understanding of using modules and using different design patterns in it.
I've also used express as my web application framework. It is a great tool and I'm glad I could be using it, not only reading about it!
I've selected MongoDB as my database, and used mongoose ORM. It really simplified a lot of things!
To get a local copy up and running follow these simple steps!
You need to have docker
and docker-compose
installed on your computer to run the service
- Clone the repo
git clone https://github.com/your_username_/Project-Name.git
- Install NPM packages
npm install
- Configure .env file
For now only the backend is avialable!
In backend directory you'll find .env.example. Create a copy of, it in the same directory, named .env
It is crucial for the app to load correctly. It contains the most important options for the application!
Since my code uses some newer js (like optional chaining), you'll need to use Node.js version at least 14.*
I've used MongoDB Docker image.
MongoDB Image
If you have your MongoDB up and running you'll need to change MONGODB_URI in .env file.
MONGODB_URI='mongodb://localhost'
I've used free and fake ethereal mails for developement.
It doesn't send any mails, but you'll be able to check what would be sent on their site.
Ethereal Mail
You'll need to change those parameters in .env file
[email protected]
EMAIL_PASSWORD=12345678
SMTP_ADDRESS=smtp.ethereal.email
SMTP_PORT=587
Go to backend
direcotry
cd backend/
Run command
docker-compose up -d
To stop the service run
docker-compose down
The backend is running on node with express.js and mongoDB.
You can import the postman collection to your instansce of Postman.
You will find collection with all requests in backend directory.
This application is simple, and so is it's structure.
Using the API you can register new users. On register event is emitted and verification email is sent to the user! You can also perform basic oprations like logging in, logging out, chaning password and email address.
Admins can also get list of all users, by id or delete them.
Links can be created even by anyone! Even by unregistred user!
When user is logged in, the link will be assign to the account.
In request you can set custom code or generate one by id. The short link generator works the same as in the cutt.ly website. I've could use n random characters generator but that wouldn't be as efficient as just generating a n characters long unique code based on id. Each time new short link is generated id is increased by one.
When someone visits the short link an event is emitted and statistics are stored in the databse. The statistic schema contains access time, referer (from header) and language (from header).
I've created many endpoints, but there are still some in developement!
Error handling
Even though we try to make our software have no errors at all, sometimes we'll run into them. Either because we made some errors in writing our app, users passing wrong data to the request or because the system just crashes. To make the life easier for me (and other developers) I've followed some rules of making RESTful API and added some error codes that will tell us if something went wrong.
Example response when we send wrong data during registration!
[100 codes] - Register codes
[101] - Username not correct! It needs to be between 5 and 16 characters and contain only standard alphabet characters, numbers and . - _
[102] - Email not correct
[103] - Password not correct! It needs to be between 8 and 24 characters. It needs to contain at least 1 number and 1 special character!
[104] - Username already taken
[105] - Email already taken
[106] - Wrong verification token
[200 codes] - Login codes
[201] - Wrong name/password combination
[202] - Account not verified
[203] - Not authenticated
[204] - No admin access
[300 codes] - Link codes
[301] - Wrong URL
[302] - Custom URL taken
[303] - Custom URL has forbidden characters
[304] - Wrong expiration date format
[305] - Expiration date passed
[306] - Link does not exists
[400 codes] - Server codes
[401] - No MongoDB connection
[402] - No LinksCounter found
[500 codes] - User codes
[501] - User does not exists
[600 codes] - Other codes
[601] - No URL found
- More link and statistic endpoints (CRUD operations)
- Query filters
- Query sorting
- Pagination
- Sending email with critical errors to admin
- Tests
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Jakub Kusiowski - @JakubKusiowski - [email protected]
Project Link: https://github.com/JKusio/ShortLink