A secure REST API built with Express and PostgreSQL. This application handles user authentication (JWT & API Keys), manages file uploads (stored directly in the database as Base64), and implements rate limiting for security.
- User System: Secure Signup and Login with password hashing (
bcrypt). - Authentication: Dual support for
Bearer Token(JWT) andx-api-key. - Database Storage: Files are stored within PostgreSQL tables (not on the disk).
- Security:
- IP and User-based Rate Limiting.
- CORS protection.
- Input validation.
- File Operations: Upload, Download (via public hash), Rename, Delete, and Update Content.
- Node.js (v18+ recommended)
- PostgreSQL Database
-
Clone the repository:
git clone <your-repo-url> cd <project-folder>
-
Install dependencies:
npm install
-
Configure Environment Variables: Create a
.envfile in the root directory and add the following:PORT=5000 # Replace with your actual Postgres connection string DATABASE_URL=postgres://username:password@localhost:5432/your_database_name JWT_SECRET=super_secret_random_key_here
-
Start the Server:
- Development (Auto-reload):
npm run dev
- Production:
node main.js
- Development (Auto-reload):
Note: The application attempts to create the necessary database tables (
usersandfiles) automatically on startup.
- GET
/- Check if the server is online.
| Method | Endpoint | Body | Description |
|---|---|---|---|
| POST | /api/signup |
{ "username": "...", "password": "..." } |
Register a new user. Returns Token & API Key. |
| POST | /api/login |
{ "username": "...", "password": "..." } |
Login to receive a Token. |
For the endpoints below, you must include one of the following headers:
- JWT:
Authorization: Bearer <your_token> - API Key:
x-api-key: <your_api_key>
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/upload |
Upload a file (Form-data: file). Max 10MB. |
🔒 Token |
| POST | /api/upload-api |
Upload a file (Form-data: file). |
🔑 API Key |
| GET | /api/files |
Get list of all uploaded files. | 🔒 Token |
| GET | /api/files-api |
Get list of all uploaded files. | 🔑 API Key |
| PUT | /api/files/:id |
Rename file. Body: { "newName": "..." } |
🔒 Token |
| DELETE | /api/files/:id |
Delete a file permanently. | 🔒 Token |
| GET | /api/files/:id/content |
Get raw Base64 content of a file. | 🔒 Token |
| PUT | /api/files/:id/content |
Update file content. Body: { "content": "base64..." } |
🔒 Token |
| Method | Endpoint | Description |
|---|---|---|
| GET | /download/:hash |
Public download link using the file hash (no auth required). |
To keep the app running permanently on a server (like VPS/EC2):
- Install PM2:
npm install -g pm2 - Start the app:
pm2 start main.js --name "file-api" - Save process:
pm2 save
This app stores file binaries in the database. Ensure your PostgreSQL instance has enough storage space, as the database size will grow linearly with file uploads.
- File Size: Max 10MB per upload.
- Rate Limit: 100 requests per 15 minutes per IP/User.