我有一个用Express构建的Nodejs应用程序,并做出反应。这个场景的js。
现在我有两个url路径'/products/:id'和'/blogs/:id'。当这些URL被facebook刮板点击时,它们会将元标记返回到facebook。
socialRouter.get('/products/:id', socialRouterController.getProduct);
socialRouter.get('/blogs/:id', socialRouterController.getBlog);
app.use(function(req, res, next) {
var ua = req.headers['user-agent'];
//console.log(ua);
if (/^(facebookexternalhit|twitterbot|WhatsApp|Pinterest|LinkedInBot|GoogleBot)/gi.test(ua)) {
console.log('social media was found');
socialRouter(req, res, next);
}
else {
//console.log('No social media crawler was found');
next();
}
})
这是直接向上的控制器代码:
getProduct: async function(req, res) {
console.log('Hello, we are sending you the html for social media sharing')
var maindomain = process.env.BASE_URL
console.log(req.params.id);
//res with data after db query
},
getBlog: async function(req, res) {
console.log('Sending blog to facebook for media sharing');
var maindomain = process.env.BASE_URL;
console.log(req.params.id);
//res with data after db query
},
我的问题是,对于博客来说,当facebook请求url时,url是完全没有定义的。
当我在facebook上点击“再刮一次”按钮时,会发生以下情况:
social media was found
Sending blog to facebook for media sharing
{ id: 'osBKKvqFzG2MJA35XOSxgXTI' }
osBKKvqFzG2MJA35XOSxgXTI
social media was found
Sending blog to facebook for media sharing
{ id: 'undefined' }
undefined
这是一种奇怪的行为,我无法解释。当我对产品页面执行完全相同的操作时,行为如下:
social media was found
Hello, we are sending you the html for social media sharing
{id: '0htUBsMbhXi7pTjAs6DqWHaE'}
social media was found
Hello, we are sending you the html for social media sharing
{id: '0htUBsMbhXi7pTjAs6DqWHaE'}
这种情况一直在发生,我无法解释。
我以为这可能是我的路线,但它们没有恶意。
<Route exact path="/products/:id" render={(routeProps) => <ProductPage {...routeProps} />} />
<Route exact path="/blogs/:id" render={(routeProps) => <PublicBlog {...routeProps} /> } />
如果有人知道为什么facebook刮板会在完全相同的条件下表现不同,请告诉我!