A simple API for resizing images using Node.js, Express, and Sharp.
This Image Processing API allows you to resize JPG images by specifying the filename, width, and height as query parameters. The API will resize the image and return the resized version. It also implements caching to avoid reprocessing the same image with the same dimensions multiple times.
- Resize images to specified dimensions
- Caching of processed images for improved performance
- Error handling for missing parameters, non-existent images, etc.
- Written in TypeScript for type safety
- Clone the repository
- Install dependencies:
npm install
- Build the project:
npm run build
Development mode:
npm run dev
Production mode:
npm start
The server will start on port 3000 by default: http://localhost:3000
Resizes an image based on the provided parameters.
Query Parameters:
filename
(required): The name of the image file (without extension)width
(required): The desired width of the resized image (in pixels)height
(required): The desired height of the resized image (in pixels)
Example:
http://localhost:3000/api/images?filename=test&width=300&height=200
Responses:
200 OK
: Returns the resized image400 Bad Request
: Missing required parameters404 Not Found
: Image not found500 Internal Server Error
: Error processing image
.
├── cache/ # Cached resized images
├── dist/ # Compiled JavaScript files
├── images/ # Original images
├── spec/ # Test files
│ ├── endpointSpec.ts # API endpoint tests
│ ├── imageSpec.ts # Image processing tests
│ └── ...
├── src/ # Source code
│ ├── controllers/ # Request handlers
│ ├── routes/ # API routes
│ ├── utils/ # Utility functions
│ └── index.ts # Application entry point
├── package.json # Project dependencies and scripts
└── tsconfig.json # TypeScript configuration
npm start
: Start the server in production modenpm run dev
: Start the server in development mode with hot reloadingnpm run build
: Compile TypeScript to JavaScriptnpm run lint
: Run ESLint to check code qualitynpm run format
: Format code using Prettiernpm test
: Run testsnpm run test:watch
: Run tests in watch mode
To add new images to the API:
- Place your JPG images in the
images/
directory - Use the filename (without extension) in your API requests
The project uses Jasmine for testing. Run the tests with:
npm test
There are two types of tests:
- Endpoint tests: Test the API endpoints and responses
- Image processing tests: Test the image resizing functionality
- Express: Web framework for Node.js
- Sharp: Image processing library
- TypeScript: JavaScript with syntax for types
- Jasmine: Testing framework
- ESLint & Prettier: Code quality and formatting tools
ISC