Image Processing API
A RESTful API service built with Node.js and TypeScript that provides image processing capabilities, specifically image resizing using the Sharp library. The API will accept image requests with specified dimensions and return processed images with caching for optimal performance.
- Create a scalable image processing API
- Implement efficient image resizing functionality
- Provide fast response times through caching
- Ensure production-ready code quality and error handling
- Demonstrate TypeScript and Node.js best practices
- Runtime: Node.js
- Framework: Express.js
- Language: TypeScript
- Image Processing: Sharp library
- Testing Framework: Jasmine
- Code Quality: ESLint + Prettier
- Package Manager: npm
express
: Web application frameworksharp
: High-performance image processing librarytypescript
: TypeScript compiler
@types/express
: TypeScript definitions for Express@types/jasmine
: TypeScript definitions for Jasmine@types/node
: TypeScript definitions for Node.jsjasmine
: Testing frameworkeslint
: Code lintingprettier
: Code formatting
- Primary: JPEG (.jpg, .jpeg)
- Optional Extensions: PNG
- Endpoint:
/api/images
- Method: GET
- Parameters:
filename
(required): Name of the source imagewidth
(optional): Target width in pixelsheight
(optional): Target height in pixels
- Response: Processed image file or error message
- Resize images to specified dimensions
- Maintain aspect ratio when only one dimension provided
- Support various resize strategies (fit, fill, cover)
- Handle edge cases for invalid dimensions
- Cache processed images to avoid reprocessing
- Implement cache key generation based on filename and dimensions
- Serve cached images for identical requests
- Cache invalidation strategies
- Validate input parameters
- Handle missing source images
- Provide meaningful error messages
- Return appropriate HTTP status codes
GET /api/images?filename=example.jpg&width=300&height=200
- Success: Binary image data with appropriate headers
- Error: JSON error message with status code
200
: Success - Image processed and returned400
: Bad Request - Invalid parameters404
: Not Found - Source image not found500
: Internal Server Error - Processing failed
assets/
│ ├── images/ # Source images
│ └── thumb/ # Processed images cache
dist/ # Compiled JavaScript
spec/
src/
├── common/
| └── BaseRoute.constants.ts
| └── common.constants.ts
├── modules/
│ └── image.processing/
│ └── image.processing.controller.ts
│ └── image.processing.routes.ts
│ └── image.processing.types.ts
├── tests/
|
├── utils/
│ └── util.process.image.ts
|
├──
└── server.ts # Main server file
.gitignore
eslint.config.mjs
package.json
README.md
tsconfig.json
- Routes: API endpoint definitions
- utils: Core business logic and image processing
- Tests: Unit and integration tests
- Assets: Image storage directories
{
"scripts": {
"check-type": "tsc --noEmit",
"dev": "tsx watch ./src/server.ts",
"build": "tsc",
"lint2": "npm run build && eslint 'index.js'",
"lint3": "eslint src/**/*.ts",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src/**/*.ts --fix",
"prettier": "npx prettier src/**/*.ts --write",
"test": "npm run build && npm run jasmine",
"jasmine": "jasmine"
}
}
tsconfig.json
: TypeScript compiler configurationspec/support/jasmine.json
: Test configuration
- Endpoint Testing: Validate API responses and status codes
- Image Processing: Test resizing functionality with various inputs
- Error Handling: Verify proper error responses
- Utility Functions: Test helper functions
- Jasmine for test framework
- Supertest for HTTP testing (optional enhancement)
- Custom test utilities for image validation
- Support for additional image formats (PNG, WebP, TIFF)
- Advanced processing features (filters, compression, format conversion)
- Batch processing capabilities
- Database integration for metadata
- Authentication and rate limiting
- Cloud storage integration
- Image optimization and compression options