Skip to content

Instantly share code, notes, and snippets.

@iofjuupasli
Created October 29, 2019 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iofjuupasli/061590c713765428d6f413923379c817 to your computer and use it in GitHub Desktop.
Save iofjuupasli/061590c713765428d6f413923379c817 to your computer and use it in GitHub Desktop.
Simplest oEmbed iframe implementation
const express = require(`express`);
const router = express.Router();
router.get(`/oembed`, (req, res) => {
const {url, maxwidth, maxheight, format} = req.query;
const width = maxwidth || 1000;
const height = maxheight || 600;
const embedUrl = url; // you can transform url that user provided to url that should be displayed in iframe
const responseData = {
type: `rich`,
version: `1.0`, // version of oEmbed protocol - current is 1.0
provider_name: `Vizydrop`, // name of your product
provider_url: `https://vizydrop.com`, // url to home page
html: `<iframe width="${width}" height="${height}" src="${embedUrl}" frameborder="0"></iframe>`, // that's what will be displayed
width,
height,
};
if (format !== `xml`) {
return res.json(responseData);
}
res.set(`Content-Type`, `text/xml`);
// you can use more "correct" way to build XML, however using template is the most simple
return res.send(
`<?xml version="1.0"?>
<oembed>
<type>rich</type>
<html>&lt;iframe width="${width}" height="${height}" src="${embedUrl}" frameborder="0" &gt;&lt;/iframe&gt;</html>
<width>${width}</width>
<height>${height}</height>
<provider_name>Vizydrop</provider_name>
<provider_url>https://vizydrop.com</provider_url>
</oembed>
`,
);
});
module.exports = router; // attach that route anywhere, that endpoint can be on any url path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment