A lightweight Node.js library to fetch link metadata โ title, description, and a highlighted image โ suitable for building link previews (for example, in chat apps, social embeds, or content summaries).
- Features
- Install
- Usage
- API Reference
- Error Handling
- Edge Cases & Limitations
- Development
- Contributing
- License
- Parse HTML of any public web page to extract:
<title>tag<meta name="description">- Open Graph image or other prominent images
- Simple callbackโbased API
- Fast, minimal dependencies
- Works in Node.js environments
npm install @thiagofeijodev/node-link-previeweror using Yarn:
yarn add @thiagofeijodev/node-link-previewerconst LinkPreview = require('@thiagofeijodev/node-link-previewer');
LinkPreview.search('https://example.com', (data) => {
console.log('Preview data:', data);
}, (err) => {
console.error('Error fetching preview:', err);
});Example output:
{
title: 'Example Domain',
description: 'This domain is for use in illustrative examples in documents.',
img: 'https://example.com/images/example.png',
link: 'https://example.com'
}| Method | Signature | Returns / Callback | Description |
|---|---|---|---|
search(url, successCallback, errorCallback) |
url (string), successCallback(data) function, errorCallback(error) function |
Calls successCallback with an object: { title, description, img, link } on success; calls errorCallback if something goes wrong |
Fetches and parses the given URL to extract metadata |
- Invalid or malformed URLs (e.g. missing protocol)
- Network issues (timeouts, DNS errors, unreachable host)
- NonโHTML responses or pages without expected tags
- Pages with no images or where images are relative paths
Wrap in Promises if you prefer async/await usage.
- Pages behind authentication will fail
- Dynamically loaded content via JavaScript is not supported
- Relative image URLs may need to be resolved manually
- Multiple images on a page โ first or OG image is chosen
- Clone the repo:
git clone https://github.com/thiagofeijodev/node-link-previewer.git
cd node-link-previewer- Install dependencies:
npm install- Run tests (when available):
npm test- Fork the repo & create a feature branch
- Write tests for new behavior
- Maintain consistent code style
- Open a pull request with clear description of changes
MIT License โ see LICENSE for details.