Express.js
Installation
1. Install the @flyyer/flyyer module
- Yarn
- NPM
yarn add @flyyer/flyyer
npm install --save @flyyer/flyyer
2. Format Flyyer CDN URLs
Use @flyyer/flyyer
to generate the smart image link on your controllers, then the req.originalUrl
to set the current pathname
dynamically.
Find your project identifier here. If you don't have a project yet, create one here.
In this example we do it for the root path (/
) but should work for any path as is.
const { Flyyer } = require("@flyyer/flyyer");
const express = require('express');
const router = express.Router();
router.get('/', function(req, res, next) {
const flyyer = new Flyyer({
project: "your-project-identifier",
path: req.originalUrl,
})
// Pass `flyyer` variables to views.
res.render('index', { title: 'Express', flyyerHref: flyyer.href() });
});
3. Render meta-tags on views
Use your favorite view engine, just make sure to render the following meta-tags in the <head />
of your pages.
<meta property="og:image" content={flyyerHref} />
<meta name="twitter:image" content={flyyerHref} />
<meta name="twitter:card" content="summary_large_image" />
If you're using Jade, set it on your layout views.
head
meta(property='og:image', content='#{flyyerHref}')
meta(name='twitter:image', content='#{flyyerHref}')
meta(name='twitter:card', content='summary_large_image')
note
If you're having trouble, please make sure you always render those meta-tags in the <head />
of your pages, and that they are not overwritten elsewhere.
4. Voilà 🎉
Now you're able to manage your link previews from your dashboard, create content from templates while preserving your brand style and export it as social media formats.
Advanced usage
Signed URLS
The module @flyyer/flyyer
supports HMAC and JWT signatures.
Find your secret key
here under Signed URLS, and enable the signing strategy you desire.
const { Flyyer } = require("@flyyer/flyyer");
const express = require('express');
const router = express.Router();
router.get('/', function(req, res, next) {
const flyyer = new Flyyer({
project: "your-project-identifier",
path: req.originalUrl,
secret: "your-secret-key",
strategy: "JWT", // or "HMAC"
})
// Pass `flyyer` variables to views.
res.render('index', { title: 'Express', flyyerHref: flyyer.href() });
});
Render your meta-tags just as in the previous section.