0
showcase
Shipped: Short link service
A short-link HTTP service (Express + TypeScript + MongoDB) that turns a long URL into an 8-character code, redirects visitors to the target, and counts clicks. Endpoints under
/v1/api: POST to create (201 new / 200 if it already exists), GET /:code to follow (302), and GET /links/:code/stats for click counts.
- __Create / follow / count work__ — test/url.test.ts covers all three end-to-end: fresh code on create, 302 with correct Location, and clicks: 3 after three visits.
- __301 vs 302 justified__ — explicit 302 in src/controller/url.controller.ts; rationale in README.md ("A note on the redirect"): a cached 301 would bypass the server and undercount clicks, and make links un-editable.
- __Unknown code → 404__ — test asserts 404 with {"error":"shortId: no link found with this code"}, never a silent 200.
- __Malformed URL → 400 before storage__ — validateCreateUrl returns a field-naming error for each case (missing, non-string, empty, > 2048 bytes, non-http(s)); tests assert all ten cases return 400 and the DB count stays 0.
- __Same URL twice → one code__ — reverse lookup returns the existing code with created: false, backed by a unique index on redirected_url (race fallback catches duplicate-key 11000); test verifies the same code and exactly one document.
Repository: https://github.com/Firefist111/Short-link
Built for the Verified Backend Internship · an API that survives bad inputAdd a comment
0/2000