How arkadipsom-code/crispurl-shortener works
Purpose and deployment shape
CrispURL is a full-stack URL shortener with two runtimes: a vanilla-JS browser SPA and an Express REST/redirect service. The frontend is deployed independently (Vercel); the backend is deployed as an API gateway (Render) backed by Neon PostgreSQL.
• Frontend entry: frontend/index.html, frontend/app.js
• Backend process entry: backend/server.js
HTTP gateway and route boundary
Express configures middleware, CORS, API route mounting, and the public dynamic redirect path in backend/server.js. URL-specific endpoint definitions are isolated from server bootstrapping in backend/routes/url.route.js.
The primary external interface is:
• Write API: POST /api/v1/urls/shorten
• Read/redirect gateway: GET /:shortCode, returning HTTP 302 for resolved links.
Controller layer: request and response policy
The controller owns HTTP-facing concerns: parsing the shortening payload, validating that longUrl is structurally valid and uses http: or https:, delegating work, and selecting status/error responses.
• backend/controllers/url.controller.js
• backend/routes/url.route.js
This separates untrusted client input and Express request objects from database-oriented business logic.
URL service: persistence and consistency
The service implements the core write and read workflows against PostgreSQL.
• backend/services/url.service.js
• backend/config/db.js
• backend/models/url.model.js
For a new URL, it first deduplicates by long_url. New records are created under a database transaction: insert to obtain the sequential database ID, derive a code, then update that row’s short_code before commit. This transaction boundary protects the ID-to-code lifecycle and avoids collision-prone random token generation.
For redirects, it resolves by indexed short_code, triggers an atomic click-count increment asynchronously, and returns the destination URL for the controller/server to redirect.
Deterministic short-code generation
Short codes are not hashes. The Base62 module converts the database-generated numeric ID into an alphanumeric token using a 62-character alphabet.
• backend/utils/base62.js
• backend/services/url.service.js
The database sequence provides uniqueness; deterministic encoding compresses that unique ID into URL-safe text. PostgreSQL’s unique short_code constraint/index is the persistence-level safeguard assumed by the design.
Database and external infrastructure
A pooled Neon PostgreSQL connection is configured in backend/config/db.js. The architecture relies on PostgreSQL transactions for writes and indexed lookups for redirect reads. The expected urls record includes the long URL, generated short code, click count, and creation timestamp, represented in backend/models/url.model.js.
No cache, queue, authentication service, or external URL-validation service is indicated. Click tracking is intentionally decoupled from redirect completion so the read path prioritizes fast 302 responses.
Frontend submission flow
The frontend presents the shortening UI and asynchronously calls the backend shortening endpoint, then renders the returned short link.
• frontend/index.html
• frontend/app.js
Its boundary is strictly API consumption: URL creation occurs server-side, while direct browser navigation to a short code is handled by the backend redirect gateway rather than the SPA.
Main entry points: url.service.js URL service · server.js HTTP gateway · url.controller.js Request controller · app.js Submission client
Components 13
Browser SPA
Independently deployed on Vercel.
- Shortening UI
frontend/index.html - Submission client · Posts URLs and renders returned short links.
frontend/app.js
Express service
REST API and public redirect gateway deployed on Render.
- HTTP gateway · Configures middleware, CORS, API mounting, and dynamic redirects.
backend/server.js - URL routes · Defines shortening API endpoints.
backend/routes/url.route.js - Request controller · Validates input and selects HTTP success or error responses.
backend/controllers/url.controller.js - URL service · Deduplicates writes and resolves redirect destinations.
backend/services/url.service.js - Base62 encoder · Encodes database IDs into deterministic short codes.
backend/utils/base62.js
PostgreSQL persistence
Neon-backed URL storage and transaction boundary.
- DB pool · Pooled Neon connection configuration.
backend/config/db.js - URL record model · Represents long URL, code, clicks, and creation time.
backend/models/url.model.js - urls table · Unique indexed short codes support redirect lookups.
- Write transaction · Insert for ID, encode it, update short code, then commit.
- Async click increment · Runs without delaying the redirect response.
Other components
- Browser navigation · Navigates directly to public short-code paths.
Connections 20
- Shortening UI → Submission client: drives
- Submission client → HTTP gateway: POST /api/v1/urls/shorten
- HTTP gateway → URL routes: mounts API
- URL routes → Request controller: dispatches
- Request controller → URL service: validated longUrl
- Browser navigation → HTTP gateway: GET /:shortCode
- HTTP gateway → Request controller: redirect handling
- URL service → DB pool: uses pool
- DB pool → urls table: connects
- URL service → urls table: deduplicates by long_url
- URL service → Write transaction: creates URL
- Write transaction → urls table: insert and update code
- Write transaction → Base62 encoder: encodes generated ID
- URL record model → urls table: maps record
- URL service → urls table: indexed short_code lookup
- URL service → Async click increment: after resolution
- Async click increment → urls table: atomic increment
- Request controller → HTTP gateway: destination or API response
- HTTP gateway → Browser navigation: 302 redirect
- Request controller → Submission client: short link response