Guest User

Untitled

a guest
Mar 19th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. const express = require('express');
  2. const fetch = require('node-fetch');
  3. const url = require('url');
  4. const app = express('');
  5.  
  6. const appUrl = 'http://xyzabc.com';
  7. const renderUrl = 'http://pqr.com/render';
  8.  
  9.  
  10. function generateUrl(request) {
  11. return url.format({
  12. protocol: request.protocol,
  13. host: appUrl,
  14. pathname: request.originalUrl
  15. });
  16. }
  17. function detectBot(userAgent){
  18. const bots = [
  19. 'bingbot',
  20. 'yandexbot',
  21. 'duckduckbot',
  22. 'slurp',
  23. //Social
  24. 'twitterbot',
  25. 'facebookexternalhit',
  26. 'linkedinbot',
  27. 'embedly',
  28. 'pinterest',
  29. 'W3C_Validator'
  30. ]
  31.  
  32. const agent = userAgent.toLowerCase();
  33.  
  34. for (const bot of bots) {
  35. if (agent.indexOf(bot) > -1) {
  36. console.log('bot detected', bot, agent);
  37. return true;
  38. }
  39. }
  40.  
  41. console.log('no bots found');
  42. return false;
  43. }
  44. app.get('*', (req, res) =>{
  45. const isBot = detectBot(req.headers['user-agent']);
  46.  
  47. if (isBot) {
  48. const botUrl = generateUrl(req);
  49.  
  50. fetch(`${renderUrl}/${botUrl}`)
  51. .then(res => res.text())
  52. .then(body => {
  53. res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
  54. res.set('Vary', 'User-Agent');
  55.  
  56. res.send(body.toString())
  57. });
  58. } else {
  59. fetch(`https://${appUrl}/`)
  60. .then(res => res.text())
  61. .then(body => {
  62. res.send(body.toString());
  63. });
  64. }
  65. });
Add Comment
Please, Sign In to add comment