|
| 1 | +/** |
| 2 | + * This is a simple server to test the server integration of the openGraph support on embed.js |
| 3 | + */ |
| 4 | + |
| 5 | +var express = require('express') |
| 6 | +var app = express() |
| 7 | +var ogs = require('open-graph-scraper'); |
| 8 | +var router = express.Router(); |
| 9 | +var timeout = require('connect-timeout') |
| 10 | + |
| 11 | +app.use(timeout(10000)) |
| 12 | +app.use(haltOnTimedout); |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +app.all('/*', function(req, res, next) { |
| 17 | + res.header("Access-Control-Allow-Origin", "*"); |
| 18 | + res.header("Access-Control-Allow-Headers", "X-Requested-With"); |
| 19 | + next(); |
| 20 | +}); |
| 21 | + |
| 22 | +router.get('/:url',function(req, res){ |
| 23 | + var options = { |
| 24 | + url : req.params.url, |
| 25 | + timeout : 2000 |
| 26 | + } |
| 27 | + |
| 28 | + ogs(options, function(err, results){ |
| 29 | + if(err){ |
| 30 | + res.json({ |
| 31 | + success:false |
| 32 | + }) |
| 33 | + } |
| 34 | + var response = results.data; |
| 35 | + console.log('YYYY:',response) |
| 36 | + if(response && response.ogTitle && response.ogDescription && response.ogUrl && response.ogImage){ |
| 37 | + var data = { |
| 38 | + success:true, |
| 39 | + title : response.ogTitle, |
| 40 | + description : response.ogDescription, |
| 41 | + url : response.ogUrl, |
| 42 | + image : response.ogImage.url |
| 43 | + } |
| 44 | + res.json(data) |
| 45 | + } |
| 46 | + |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +app.use('/', router) |
| 51 | + |
| 52 | +function haltOnTimedout(req, res, next){ |
| 53 | + if (!req.timedout) next(); |
| 54 | +} |
| 55 | + |
| 56 | +var server = app.listen(3000, function(){ |
| 57 | + var host = server.address().address |
| 58 | + var port = server.address().port; |
| 59 | + |
| 60 | + console.log('Example app listening at http://%s:%s', host, port); |
| 61 | +}) |
0 commit comments