A Node.js utility service that provides Open Graph image generation and website thumbnail extraction capabilities. Built with Express.js and optimized for deployment on Vercel.
- Open Graph Image Generation: Dynamically generate social media preview images with custom user data
- Website Thumbnail Extraction: Extract Open Graph and Twitter Card images from any URL
- Intelligent Caching: Multi-layer caching system with memory and file-based storage
- Device Detection: Built-in user agent parsing for responsive behavior
- SVG to PNG Conversion: Uses Satori and Resvg for high-quality image rendering
GET /og?name={name}&username={username}&bio={bio}&photo={photo_url}
Generates a custom Open Graph image with user information.
Parameters:
name- User's display nameusername- User's username/handlebio- User's bio/descriptionphoto- Profile photo URL (optional, defaults to placeholder)
Example:
https://your-domain.com/og?name=John%20Doe&username=johndoe&bio=Software%20Engineer&photo=https://example.com/photo.jpg
Response: PNG image (1200x630px)
GET /thumbnail?url={website_url}
Extracts the main image from a website's Open Graph or Twitter Card meta tags.
Parameters:
url- Target website URL (automatically adds https:// if missing)
Example:
https://your-domain.com/thumbnail?url=github.com
Response:
{
"url": "github.com",
"image": "https://github.githubassets.com/images/modules/site/social-cards/campaign-social.png"
}GET /cache/stats
Returns cache performance statistics.
DELETE /cache
Clears all cached data (useful for development).
- Clone the repository:
git clone <repository-url>
cd glance-utils- Install dependencies:
npm install- Create environment file (optional):
cp .env.example .env- Start the development server:
npm run devThe server will start on port 4000 (or the port specified in the PORT environment variable).
This project is optimized for Vercel deployment with the included vercel.json configuration.
- Install Vercel CLI:
npm i -g vercel- Deploy:
vercelThe service can be deployed to any Node.js hosting platform. Make sure to:
- Set the
NODE_ENV=productionenvironment variable - Ensure the platform supports Node.js 16+
- Configure any necessary build commands
PORT- Server port (default: 4000)NODE_ENV- Environment (development/production)
The cache system can be configured in the CacheManager initialization:
const cache = new CacheManager({
ttl: 3600, // Cache TTL in seconds (1 hour)
enableFileCache: true, // Enable persistent file cache
maxFileCacheSize: 200, // Maximum number of cached files
});- Runtime: Node.js
- Framework: Express.js
- Image Generation: Satori + Resvg
- HTML Parsing: Cheerio
- Caching: node-cache + file system
- Font Loading: Inter font family from CDN
The service implements a two-tier caching system:
- Memory Cache: Fast in-memory storage using node-cache
- File Cache: Persistent storage for longer-term caching
- Automatic Cleanup: Removes old cache files when limits are exceeded
- Parse query parameters
- Check cache for existing image
- Generate HTML template with user data
- Convert HTML to SVG using Satori
- Convert SVG to PNG using Resvg
- Cache the result
- Return PNG buffer
glance-utils/
├── index.js # Main server file
├── cache.js # Cache management system
├── package.json # Dependencies and scripts
├── vercel.json # Vercel deployment config
├── tsconfig.json # TypeScript configuration
└── .cache/ # File cache directory
The service is designed to be easily extensible. Key areas for enhancement:
- Templates: Modify the HTML template in the
/ogendpoint - Styling: Update the SVG styling and layout
- Caching: Adjust cache strategies in
cache.js - Endpoints: Add new utility endpoints following the existing pattern
Test the service locally:
# Start development server
npm run dev
# Test OG image generation
curl "http://localhost:4000/og?name=Test&username=test&bio=Testing"
# Test thumbnail extraction
curl "http://localhost:4000/thumbnail?url=github.com"
# Check cache stats
curl "http://localhost:4000/cache/stats"- Caching: Reduces image generation time by 90%+ for repeated requests
- Font Preloading: Inter fonts are loaded once and cached
- Optimized Rendering: Satori provides fast HTML-to-SVG conversion
- Memory Management: Automatic cache cleanup prevents memory leaks
ISC
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
For issues and questions, please create an issue in the repository or contact the maintainers.