-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (63 loc) · 2.34 KB
/
index.js
File metadata and controls
80 lines (63 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const axios = require('axios');
function getResponseBody(data){
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>${data.title}</title>
<!-- Search Engine -->
<meta name="description" content="${data.description}">
<meta name="image" content="${data.imgSrc}">
<!-- Schema.org for Google -->
<meta itemprop="name" content="${data.title}">
<meta itemprop="description" content="${data.description}">
<meta itemprop="image" content="imgSrc">
<!-- Open Graph general (Facebook, Pinterest & Google+) -->
<meta prefix="og: http://ogp.me/ns#" name="og:type" content="website">
<meta prefix="og: http://ogp.me/ns#" name="og:title" content="${data.title}">
<meta prefix="og: http://ogp.me/ns#" name="og:description" content="${data.description}">
<meta prefix="og: http://ogp.me/ns#" name="og:image" content="${data.imgSrc}">
<meta prefix="og: http://ogp.me/ns#" name="og:url" content="${data.url}">
<meta prefix="og: http://ogp.me/ns#" name="og:site_name" content="${data.title}">
</head>
<body>
<p>name: ${data.title}</p>
<p>description: ${data.description} </p>
</body>
</html>`
}
module.exports.handler = async (event, context, callback) => {
// Get the request from cloudfront
let request = event.Records[0].cf.request
let headers = request.headers
let useragent = JSON.stringify(headers["user-agent"][0].value)
console.log('Request UA: '+useragent)
// Check for the following useragent
let botUserAgentPattern = new RegExp(process.env.BOT_USER_AGENT)
let isBotUserAgent = botUserAgentPattern.test(useragent)
// for non-bot request
if (!isBotUserAgent) {
console.log('Non-bot UA.');
callback(null, request);
return
}
// Construct http response for social sites.
console.log('Bot UA: ');
const res = await axios.get(process.env.EXTERNAL_API);
// Load res into the data obj
let data = {
title: 'title',
description: 'description',
imgSrc: 'imgSrc',
url: 'url'
};
console.log('ResponseData: '+JSON.stringify(data))
let responseBody = getResponseBody(data)
const response = {
status: '200',
statusDescription: 'OK',
body: responseBody
};
// Return reponse
callback(null, response);
};