Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blackbing/e0a913af1b376016ed003b97f2d240f5 to your computer and use it in GitHub Desktop.
Save blackbing/e0a913af1b376016ed003b97f2d240f5 to your computer and use it in GitHub Desktop.
'use strict';
const http = require('http');
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const response = event.Records[0].cf.response;
if (response.status == 404) {
const domainHash = request.headers['x-domain-hash'] && request.headers['x-domain-hash'][0].value;
if (domainHash) {
const host = request.headers['host'][0].value;
const url = `http://${host}/${domainHash}/index.html`;
console.log(url);
downloadContent(url, function(body, error) {
if (error) {
console.log(error)
callback(null, response);
} else {
const newResponse = {
status: 200,
statusDescription: 'OK',
headers: response.headers,
body
};
newResponse.headers['content-type'] = [{
key: 'Content-Type',
value: 'text/html'
}];
callback(null, newResponse);
}
})
}
} else {
callback(null, response);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment