Run Commands
Set environment variable ENV=development (if not then it ask you to after running npm start and test will fail). Uses ENV to pick correct configs. Can be production as well but the configs aren't setup for obvious reasons.
npm install
npm start
Apis will run on port:1337
For Tests
npm test
POSTMAN COLLECTION
Things I've accomplished
- GET api for shortening the URL
- POST api for check how many times a particular URL was shortened
- Relevant test cases for both the apis
- Database being used is MySQL. I'm using free service on freemysqlhosting.net
Description
CODE
- Code for the apis is in src/models/Url.js
- Code for the routes is in config/routes/v1.js
- Code for the tests is in test/app.test.js
- Code for DB configs is in config/env/.js
Database
- It has two tables: url and shortened_url
- url: (id, url, created_at). ID is auto_increment primary key and created_at is current date_time stamp and url is the url passed that needs to be shortened.
- shortened_url: (id, url_id, shortened_url, user, created_at). ID is auto_increment primary key, created_at is current date_time stamp, url_id is id of url for which this record is being created, shortened_url is the shortened version of url passed and user field can store any meta data about user or can be NULL.
Shortening URL API
- This api first checks if the url is present or not in params. Then uses regex to validate if url passed is valid or not.
- Then it checks if the url record was already created or not. If not then create the record.
- Create a random and unique url which is accomplished in mysql query itself. It'll prevent collisions
- Then it just returns just the shortened URL.
URL usage API
- This api first checks if the url is present or not in params. Then uses regex to validate if url passed is valid or not.
- Fetches the count from the DB for the number of times that particular url has been shortened.
- Then it just returns just the shortened URL.
Tests
- I'm using mocha and chai for the test.
- I've added 7 relevant tests for the APIs.