Nodejs动态OG标签

时间:2014-08-25 02:53:21

标签: node.js express facebook-opengraph

使用Express。这是路线。它基本上只是将url params传递给一个呈现OG标记的模板。

router.get('/share/:redirectURL/:title/:description/:img', function (req, res) {

    var url = req.protocol + '://' + req.get('host') + req.originalUrl; // points to this endpoint

    res.render('share', {
        url: url,
        title: decodeURIComponent(req.params.title),
        img: decodeURIComponent(req.params.img),
        description: decodeURIComponent(req.params.description),
        redirectURL: decodeURIComponent(req.params.redirectURL)
    });

});

module.exports = router;

这是它呈现的共享模板。

doctype html
html
    head
        meta(property="og:url", content="#{url}")
        meta(property="og:image", content="#{img}")
        meta(property="og:title", content="#{title}")
        meta(property="og:description", content="#{description}")
        meta(property="og:type", content="article")

    body
        script.
            location.replace("#{redirectURL}");

......它有效!但它只能在局部工作。一旦我上传到服务器,事情就会出错。

有效:http://localhost/share/http%3A%2F%2Fgoogle.com/Hear%20some%20music./http%3A%2F%2F201.23.456.789%2F%2Fassets%2Fimgs%2Ffavicons%2Ficon1024.png

无效:http://123.45.678.910/share/http%3A%2F%2Fgoogle.com/Hear%20some%20music./http%3A%2F%2F201.23.456.789%2F%2Fassets%2Fimgs%2Ffavicons%2Ficon1024.png

上游的某些内容在它到达Express路由器之前部分解码了URL。结果是这个混乱,无用的东西。

http://123.45.678.910/share/http:/google.com/Hear%20some%20music./http%3A%2F%2F201.23.456.789%2F%2Fassets%2Fimgs%2Ffavicons%2Ficon1024.png

1 个答案:

答案 0 :(得分:1)

切换到查询参数,它可以工作!