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
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Optimize Your Links For Print Using CSS — Show The URL

    When moving around from page to page in your trusty browser, you get the benefit of hovering over links and viewing the link's target URL in the status bar. When it comes to page printouts, however, this obviously isn't an option. Most website printouts...

  • By
    CSS Columns

    One major gripe that we've always had about CSS is that creating layouts seems to be more difficult than it should be. We have, of course, adapted and mastered the techniques for creating layouts, but there's no shaking the feeling that there should be a...

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!