Created
October 29, 2019 12:46
-
-
Save iofjuupasli/061590c713765428d6f413923379c817 to your computer and use it in GitHub Desktop.
Simplest oEmbed iframe implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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><iframe width="${width}" height="${height}" src="${embedUrl}" frameborder="0" ></iframe></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