Shortlink Express is a lightweight, secure RESTful API designed to manage URL shortening services. Built with Node.js, Express, and Prisma ORM, it provides robust user authentication and efficient link management capabilities using a MySQL database.
- Runtime: Node.js
- Framework: Express.js (v5)
- Database: MySQL
- ORM: Prisma
- Authentication: JWT (JSON Web Tokens) & HTTP-Only Cookies
- Security: Bcrypt (Password Hashing), CORS
- User Authentication:
- Secure Register and Login.
- Session management via HTTP-Only Cookies.
- Password encryption using Bcrypt.
- Link Management:
- Create short links with auto-generated or custom slugs.
- Edit existing destination URLs and slugs.
- Delete links.
- View all links associated with the authenticated user.
- Data Integrity:
- Unique constraints on Usernames and Slugs.
- Relational mapping between Users and Links.
To run this project, you will need to add the following environment variables to your .env file in the root directory:
| Variable | Description | Example |
|---|---|---|
DATABASE_URL |
Connection string for your MySQL database | mysql://user:pass@localhost:3306/db_name |
JWT_SECRET |
Secret key for signing JSON Web Tokens | your_super_secret_key |
NODE_ENV |
Environment mode (development/production) | development |
Follow these steps to set up the project locally.
git clone https://github.com/inirizky/shortlink-express.git
cd shortlink-expressnpm installEnsure you have MySQL running. Then, generate the Prisma client and push the schema to your database.
# Generate Prisma Client
npx prisma generate
# Run Migrations
npx prisma migrate dev --name init# Start in development mode (using nodemon)
npm startThe server will start on http://localhost:3001.
shortlink-express/
├── controller/ # Business logic for Users and Links
├── middleware/ # Authentication middleware
├── prisma/ # Database schema and migrations
├── routes/ # API route definitions
├── index.js # App entry point and configuration
├── package.json # Dependencies and scripts
└── .env # Environment variables
Register User
POST /api/users/register
Content-Type: application/json
{
"username": "johndoe",
"password": "password123",
"fullname": "John Doe"
}Login User
POST /api/users/login
Content-Type: application/json
{
"username": "johndoe",
"password": "password123"
}Create Shortlink (Requires Auth Cookie)
POST /api/links/new
Content-Type: application/json
{
"url": "https://www.google.com",
"slug": "goog"
}
// Note: "slug" is optional. If omitted, a random string is generated.Get User's Links (Requires Auth Cookie)
GET /api/linksContributions are welcome! Please follow these steps:
- Fork the project.
- Create your Feature Branch (
git checkout -b feature/AmazingFeature). - Commit your Changes (
git commit -m 'Add some AmazingFeature'). - Push to the Branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
This project is licensed under the ISC License.