node express routing decision based on user-agent
<p>Trying to figure out a way of supplying better data to social media (open graph data). Basically, when facebook, twitter or pinetrest asks for information about a link on my page, I want to provide them og information dependent on link instead of sending them the empty page (OK, it sends javascripts that they dont run). </p>

<p>I tried using prerender and similar, but cant get that to run propperly. But I also realised that I would rather get the express router to identify it and service a static page based on the request. </p>

<p>As a first step, I need to get the user agent information:</p>

<p>So I thought I would add express-useragent, and that seems to work on my test site, but does not seem like facebooks scraper ever goes past it. I can see it tries to get a picture, but never updates the OG or the index. (code below should work as an example) </p>

<pre><code>var express = require('express');
var router = express.Router();
var useragent = require('express-useragent');



//Set up log
var cfgBunyan = require('../config/bunyan')
var log = cfgBunyan.dbLogger('ROUTE')


router.use(useragent.express());
/* GET home page. */
router.get('/', function(req, res, next) {

  console.log(req.useragent);
  res.render('index');
});

router.get('/share/:service', function(req, res, next) {
  res.render('index');
});

router.get('/pages/:name', function (req,res, next){
  log.info('/pages/'+req.params.name)
  res.render('pages/'+req.params.name);
});
router.get('/modals/:name', function (req,res, next){
  res.render('modals/'+req.params.name);
});


router.get('/page/:name', function (req,res, next){
  res.render('index');
});


module.exports = router;
</code></pre>

<p>I can also tun the google test scraper, which gives me the following source</p>

<pre><code>source: 'Mozilla/5.0 (compatible; Google-Structured-Data-Testing-Tool +https://search.google.com/structured-data/testing-tool)' }
</code></pre>

<p>So has anyone figured out a easy way to direct facebook and twitter to another route? Or is sitting and checking the different sources the right way?</p>