A full-stack URL shortener application built with Node.js, React, and PostgreSQL.
- Shorten long URLs with custom or auto-generated slugs
- Server-side redirects (SEO & social media crawler friendly)
- Click analytics and tracking
- Recent links history
- Copy-to-clipboard support
- Responsive UI
- Backend: Node.js, Express.js
- Database: PostgreSQL
- Frontend: React, Vite, Tailwind CSS
id SERIAL PRIMARY KEY,
slug VARCHAR(255) UNIQUE NOT NULL,
long_url TEXT NOT NULL,
clicks INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMPid SERIAL PRIMARY KEY,
slug VARCHAR(255),
ip VARCHAR(45),
user_agent TEXT,
referer TEXT,
country VARCHAR(10),
location TEXT,
is_bot BOOLEAN DEFAULT false,
device_info JSONB,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP- Node.js (v14 or higher)
- npm or yarn
- PostgreSQL (v12 or higher)
-
Navigate to the
serverdirectory:cd server -
Install dependencies:
npm install
-
Set up PostgreSQL:
- Create a PostgreSQL database
- Run the SQL schema provided above
- Note your database credentials
-
Create a
.envfile in theserverdirectory:PORT=3000 BASE_URL=http://localhost:3000 DB_HOST=localhost DB_PORT=5432 DB_NAME=your_database_name DB_USER=your_db_user DB_PASSWORD=your_db_password
-
Start the server:
npm start
-
Navigate to the
clientdirectory:cd client -
Install dependencies:
npm install
-
Start the development server:
npm run dev
POST /api/shorten– Create a shortened URLGET /:slug– Redirect to the original URLGET /api/recent– Get recently created URLsGET /api/stats/:slug– Get analytics for a specific URL
PORT– Server port (default: 3000)BASE_URL– Base URL for generating short linksDB_HOST– PostgreSQL hostDB_PORT– PostgreSQL portDB_NAME– Database nameDB_USER– Database userDB_PASSWORD– Database password
For production deployment:
-
Build the frontend:
npm run build
-
Serve the frontend build via:
- Nginx
- Express static files
- CDN (optional)
-
Set production environment variables on the server
-
Use a reverse proxy (Nginx/Apache) if required
-
Enable HTTPS for security
- Never commit
.envfiles to version control - Use strong database credentials
- Implement rate limiting to prevent abuse
- Sanitize inputs to avoid SQL injection
- Consider authentication for managing personal links
Contributions, issues, and feature requests are most welcome.