A URL shortener built with TypeScript and Node.js. It uses a PostgreSQL database and focuses on the essential functionality of converting long URLs into shortened versions and redirecting users based on the generated short codes. This project also supports user authentication and provides admin-only analytics features.
-
User Authentication:
Register and log in users using JWTs. See src/routes/userRoutes.ts. -
URL Shortening & Redirection:
Shorten URLs for logged-in users and redirect using unique short codes. See src/routes/urlRoutes.ts. -
Admin Analytics:
View various analytics metrics Accessible via src/routes/adminRoutes.ts.
- package.json: Project metadata, dependencies, and scripts.
- tsconfig.json: TypeScript compiler configuration.
- src/app.ts: Application entry point.
- src/database.ts: PostgreSQL database connection and schema initialization.
- src/middleware/authMiddleware.ts: JWT-based authentication middleware.
- src/routes/userRoutes.ts: Endpoints for user registration and login.
- src/routes/urlRoutes.ts: Endpoints for URL shortening and redirection.
- src/routes/adminRoutes.ts: Endpoints for admin analytics.
- src/utils/urlUtils.ts: Utility function to validate URLs.
- openapi.yaml: OpenAPI Specification for API documentation.
- tests/: Contains unit and integration tests.
-
Install Dependencies:
Run the following command in your terminal from the project root:
npm install
-
Configure Environment Variables:
Modify a .env file (or create a new one) with the following content:
DB_HOST=localhost DB_PORT=5432 DB_NAME=url_shortener DB_USER=your_db_username DB_PASSWORD=your_db_password JWT_SECRET=your_super_secret_key_here
-
Initialize the Database Schema:
The schema is automatically initialized when the application starts via database.ts:
Start the application with:
npx ts-node src/app.tsThe API is fully documented using OpenAPI. Render openapi.yaml with a tool like Swagger UI or Redoc.
Execute the tests using:
npm test