Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const fetch = require('node-fetch');
- const url = require('url');
- const app = express('');
- const appUrl = 'http://xyzabc.com';
- const renderUrl = 'http://pqr.com/render';
- function generateUrl(request) {
- return url.format({
- protocol: request.protocol,
- host: appUrl,
- pathname: request.originalUrl
- });
- }
- function detectBot(userAgent){
- const bots = [
- 'bingbot',
- 'yandexbot',
- 'duckduckbot',
- 'slurp',
- //Social
- 'twitterbot',
- 'facebookexternalhit',
- 'linkedinbot',
- 'embedly',
- 'pinterest',
- 'W3C_Validator'
- ]
- const agent = userAgent.toLowerCase();
- for (const bot of bots) {
- if (agent.indexOf(bot) > -1) {
- console.log('bot detected', bot, agent);
- return true;
- }
- }
- console.log('no bots found');
- return false;
- }
- app.get('*', (req, res) =>{
- const isBot = detectBot(req.headers['user-agent']);
- if (isBot) {
- const botUrl = generateUrl(req);
- fetch(`${renderUrl}/${botUrl}`)
- .then(res => res.text())
- .then(body => {
- res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
- res.set('Vary', 'User-Agent');
- res.send(body.toString())
- });
- } else {
- fetch(`https://${appUrl}/`)
- .then(res => res.text())
- .then(body => {
- res.send(body.toString());
- });
- }
- });
Add Comment
Please, Sign In to add comment