expressjs node.js serve different data to google/etc bot and human traffic

微笑、不失礼 提交于 2019-12-10 15:17:20

问题


I want to determine if incoming requests are from a bot (eg google, bing), or a human, and serve different data to each, for example, json data for client javascript to construct the site or preprocessed html.

Using expressjs, is there an easy way to do this? Thanks.


回答1:


I recommend you to response according to the requested MIME type (which is present in the "Accept" header). You can do this with Express this way:

app.get('/route', function (req, res) {
    if (req.is('json')) res.json(data);
    else if (req.is('html')) res.render('view', {});
    else ...
});



回答2:


You can check the req.header('User-Agent') for 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html'. If it's that you know it's Google and can send it different data.

http://www.google.com/support/webmasters/bin/answer.py?answer=1061943

How to get headers http://expressjs.com/4x/api.html#req.get



来源:https://stackoverflow.com/questions/7508758/expressjs-node-js-serve-different-data-to-google-etc-bot-and-human-traffic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!