oEmbed provider middleware for Node.js Connect and Express
oEmbed provider middleware for Node.js Connect and Express. Adds support to Connect/Express for creating an oEmbed provider API (ie
connect-oembed is available via npm:
npm install connect-oembed
var connect = require'connect';var oembed = require'connect-oembed';var app = connect;appuse'/oembed' oembed// determine your embed details here based on query params in req.oembed// say we're oembedding images from <image id>var urlRegEx = /^http:\/\/example\.com\/images\//;var matched = urlRegExexecreqoembedurl;if matched != nullvar imageId = matched1;// here you could look up some meta from a database, for now we can just// return some made up values using the res.oembed.photo() helperresoembedphoto"" + imageId // url256 // width256; // height// the helper automatically builds and sends the response for you// so there's nothing left to do hereelsenext;;applisten8080;
After adding the connect-oembed middleware to your app, most interactions will be through the objects attached to the req and res objects. oEmbed request parameters (url, format, etc...) will be available via the req.oembed object (ie. req.oembed.url). To respond to an oEmbed request, use the helpers provided by the res.oembed object. The module will automatically respond in the correct format (JSON or XML) based on the request, choosing JSON if no format is specified. Any custom options passed to the helper will also be encoded into the json or xml response.
var oembed = require'connect-oembed';/*** attach the connect-oembed middleware at the specified path*/appuse"/path/to/api/oembed" oembed/*** responds to an oEmbed request with a photo.* all options will be encoded into the response.*/resoembedphotourl width height <options>;/*** responds to an oEmbed request with a video.* all options will be encoded into the response.*/resoembedvideohtml width height <options>;/*** responds to an oEmbed request with a link (only takes options).* all options will be encoded into the response.*/resoembedlink<options>;/*** responds to an oEmbed request with rich content.* all options will be encoded into the response.*/resoembedrichhtml width height <options>;;
Even though it's not specified by oembed.com, connect-oembed supports wrapping a JSON response as a JSONP response. Just append a callback or jsonp parameter to the GET query and connect-oembed will respond accordingly. For example the following request would return a JSONP response: