admin管理员组文章数量:1410724
I want to determine if ining 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.
I want to determine if ining 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.
Share asked Sep 22, 2011 at 2:00 HarryHarry 55.1k76 gold badges187 silver badges270 bronze badges 2- FYI, search engines tend to not like when they get substantially different content from what a normal client gets. – icktoofay Commented Sep 22, 2011 at 2:07
- @icktoofay it's the same content, if you read google's ajax documentation they expressly allow for this – Harry Commented Sep 22, 2011 at 2:29
3 Answers
Reset to default 4You can check the req.header('User-Agent') for 'Mozilla/5.0 (patible; Googlebot/2.1; +http://www.google./bot.html'. If it's that you know it's Google and can send it different data.
http://www.google./support/webmasters/bin/answer.py?answer=1061943
How to get headers http://expressjs./4x/api.html#req.get
I remend 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 ...
});
Checking for request header User-Agent
or MIME type as suggested is not reliable, since any HTTP GET request can define User-Agent
and headers at will.
The most reliable and secure approach is to check by IP.
Therefore I developed an NPM package that does exactly that. It stores at startup in-memory all known IP ranges ing from Google bots and crawlers, for very fast middleware processing.
const express = require('express')
const isGCrawler = require('express-is-googlecrawler')
const app = express()
app.use(isGCrawler)
app.get('/', (req, res) => {
res.send(res.locals.isGoogleCrawler) // Boolean
})
app.listen(3000)
本文标签: javascriptexpressjs nodejs serve different data to googleetc bot and human trafficStack Overflow
版权声明:本文标题:javascript - expressjs node.js serve different data to googleetc bot and human traffic - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744814365a2626606.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论