Get Open Graph Data with Node.js

By  on  

Two of this blog's most popular posts are Facebook Open Graph META Tags and How to Create a Twitter Card.  I'm not at all surprised because we as content creators want some visual control over how our site is represented and shared on third party sites, especially social media sites.   I was recently posting a link on Tech.pro and they instantly grab open graph information about the page, prepopulating known information, just like Facebook and Twitter, and that got me thinking about the other side of open graph data -- scraping data from a site.  Look no further than the open-graph-scraper JavaScript module!

After installing the module from NPM or GitHub, getting Open Graph information from a given URL is easy:

var ogs = require('open-graph-scraper');

ogs(
	{ url: 'https://davidwalsh.name' }, // Settings object first
	function(er, res) { console.log(er, res); }  // Callback 
);

/*  Result:
	
	{ data:
	   { success: 'true',
	     ogImage: 'https://davidwalsh.name/wp-content/themes/punky/images/logo.png',
	     ogTitle: 'David Walsh - JavaScript, HTML5 Consultant',
	     ogUrl: 'https://davidwalsh.name/',
	     ogSiteName: 'David Walsh Blog',
	     ogDescription: 'David Walsh Blog features tutorials about MooTools, jQuery, Dojo, JavaScript, PHP, CSS, HTML5, MySQL, and more!' },
	  success: true }
	
*/

Simple API and simple result -- lovely.  Of course every programming language will have an equivalent library but we all know JavaScript is king!  Happy sharing everyone!

Recent Features

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Six Degrees of Kevin Bacon Using MooTools 1.2

    As you can probably tell, I try to mix some fun in with my MooTools madness but I also try to make my examples as practical as possible. Well...this may not be one of those times. I love movies and useless movie trivia so naturally I'm...

  • By
    Simple Image Lazy Load and Fade

    One of the quickest and easiest website performance optimizations is decreasing image loading.  That means a variety of things, including minifying images with tools like ImageOptim and TinyPNG, using data URIs and sprites, and lazy loading images.  It's a bit jarring when you're lazy loading images and they just...

Discussion

  1. Muneeb

    Hi, David Thank you for this post. When using this code to fetch from multiple links . It is giving unhandled promise rejection.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!