This project is an Image Optimization Service built using Node.js and Express. It leverages the Sharp library to perform image transformations such as format conversion, resizing, and quality adjustments. The service is designed to efficiently serve optimized images based on client requests, improving load times and reducing bandwidth usage.
- Dynamic image format conversion (e.g., JPEG, PNG, WebP, AVIF)
- Image resizing capabilities
- Quality adjustment for optimized images
- Caching mechanism to store and retrieve optimized images
- Configurable source directories for image retrieval
cdn.api.pphat.stackdev.cloud/
├── images/ # Static assets (if any)
├── uploads/ # Static assets (if any)
├── src/
│ ├── controllers/ # Main controller handling image requests and processing
│ │ └── images.controller.ts # Image processing logic using Sharp
│ ├── utils/
│ │ ├── config.ts # Configuration utilities for source directories
│ │ ├── files.ts # File handling utilities
│ │ └── image-cache.ts # Caching utilities for optimized images
│ └── app.ts # Express app setup
├── dist/ # Compiled output directory (after build)
├── package.json # Project metadata and dependencies
├── tsenv.json # TypeScript configuration
├── README.md # Project documentation
└── .gitignore # Git ignore file- Node.js (version 18 or higher)
- npm (Node Package Manager)
- Clone the repository:
git clone https://github.com/pphatdev/cdn.api.pphat.stackdev.cloud.git- Navigate to the project directory:
cd cdn.api.pphat.stackdev.cloud- Install the dependencies:
npm install- Create a configuration file (e.g.,
env.json) in the root directory to specify source directories and other settings. - Define the source directories where images are stored.
{
"port": 3000,
"sourceDirectories": [
"./images",
"./uploads"
]
}To run the service in development mode with automatic restarts on file changes, use:
npm run devAfter building the project, the compiled files will be located in the dist directory as ES modules.
To build the project, run:
npm run buildStart the server using the following command:
npm startThe server will start on the configured port (default is 3000). You can access the service at http://localhost:3000.