A file management application built with Node.js, Express.js, MongoDB, Redis, and Docker. This project allows users to upload, manage, and serve files while supporting background tasks like generating image thumbnails.
- Project Overview
- Features
- Technologies Used
- Getting Started
- Project Structure
- Environment Variables
- Docker Usage
- API Documentation
- License
The File Manager application provides an API to handle file uploads, file retrieval, user authentication, and more. It uses MongoDB for file storage and Redis for caching and session management. The entire project is containerized using Docker.
- User Authentication: Allows users to sign up, log in, and authenticate using JWT tokens.
- File Upload & Management: Users can upload files, create folders, and organize files hierarchically.
- Image Thumbnail Generation: Background jobs (using Bull) generate thumbnails for image files.
- Public & Private Files: Users can make files either public or private.
- Dockerized: The project is fully containerized with Docker for easy deployment.
- Node.js: Backend server and API.
- Express.js: Web framework for building the REST API.
- MongoDB: NoSQL database for file storage.
- Redis: Caching and session storage.
- Docker: For containerization.
- Bull: Queue system for background jobs (e.g., image thumbnail generation).
- JWT: For user authentication and session management.
- Docker: Ensure Docker is installed. You can download Docker from here.
- Node.js: If running outside of Docker, install Node.js from here.
-
Clone the repository:
git clone https://github.com/your-username/file-manager.git cd file-manager
-
Build and start the Docker containers:
docker-compose up --build
This will start the app, MongoDB, Redis, and the worker service.
-
Once the services are running, you can access the app at
http://localhost:3000
.
-
server.js
The main entry point to the application. It initializes the app, configures the middleware, and starts the server on a specified port. -
/Controllers
Contains all the API controllers that manage the routes and logic for different resources:AppController.js
: Handles application-level API endpoints.AuthController.js
: Manages user authentication (e.g., login, registration).FileController.js
: Handles file-related operations like uploading, downloading, and managing files.UsersController.js
: Manages user-specific operations like user profile retrieval and deletion.
-
/Utils
Utility functions and helper modules:db.js
: Functions for interacting with MongoDB, such as database connections and queries.redis.js
: Functions for interacting with Redis, such as caching and session storage.mailer.js
: Contains functions for sending emails (e.g., account confirmation, notifications).getUserId.js
: Utility to handle user authentication and extract user information from requests.
-
/routes
Destructured API endpoints for organizing and routing requests:index.js
: Contains all the defined API routes, which connect controllers with the corresponding paths.
-
DockerFile
The Dockerfile defines the instructions for building the Docker image for the application, including dependencies and configuration. -
docker-compose.yml
Configuration file for Docker Compose to define the multi-container setup for the application, including services like the app, database, and cache. -
worker.js
A utility script to handle background tasks such as processing queues or performing asynchronous operations.
This structure ensures that the code is modular, scalable, and easy to maintain. Each component is logically separated into specific folders, making it easy to navigate and extend the application.
The application uses the following environment variables, which should be set in a .env
file:
- DB_HOST: MongoDB host (default:
mongodb
) - DB_PORT: MongoDB port (default:
27017
) - DB_DATABASE: MongoDB database name (default:
files_manager
) - REDIS_HOST: Redis host (default:
redis
) - REDIS_PORT: Redis port (default:
6379
) - APP_PORT: Application port (default:
3000
)
-
Building the App:
docker-compose build
-
Running the Containers:
docker-compose up
-
Stopping the Containers:
docker-compose down
-
Accessing Services: After starting the containers, you can access the app at:
- App:
http://localhost:3000
- MongoDB:
mongodb://localhost:27017
- Redis:
localhost:6380
- App:
- Summary: Check the server status.
- Tags: App
- Response:
200 OK
:
{"redis":true, "db":true }
- Summary: Get application statistics.
- Tags: App
- Response:
200 OK
:
{ "users":4, "files":30 }
- Summary: Create a new user.
- Tags: Users
- Request Body:
{ "email": "string", "password": "string" }
- Response:
200: OK
: Returns user details
- Summary: Generates a token for user to login.
- Tags: Users
- Request body:
{ "email": "string", "password": "string" }
- Response:
200: OK
:{ "token":"uuid-string" }
- Summary: Logout the user.
- Tags: Users
- Request body:
{ "email": "string", "password": "string" }
- Response:
200: OK
:
- Summary: Retrieves user Information.
- Tags: Users
- Request header:
{ "x-token": "string", }
- Response:
200: OK
:{ "id":"string", "email":"string" }
- Summary: Upload a file.
- Tags: Files
- Request header:
{ "x-token": "string", }
- Request body:
{ "name": "string", "type": ["file","folder","image"], /* Can be one of this*/ "data": "SGVsbG8gV2Vic3RhY2shCg==", "parentId": "Interger", "isPublic": "boolean", }
- Response:
200:OK
:{ "id":"string", "userId":"String", "name":"String", "type":"String", "isPublic": "boolean", "parentId": "integer" }
This markdown format cleanly organizes the API endpoints and responses, making it easy to read and reference for your project's documentation.