Created
July 17, 2017 08:03
-
-
Save faermanj/e104d02241072e245c1f02569242d804 to your computer and use it in GitHub Desktop.
URL Shortener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const data = require('./data'); | |
const response = (statusCode, url) => { | |
return { | |
statusCode: statusCode, | |
headers: { | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Credentials': true, | |
'Location': `${url}` | |
}, | |
body: '' | |
}; | |
}; | |
const pathOf = (path) => | |
new Promise((resolve) => { | |
resolve(`${path}`.split('/').slice(-1)[0]); | |
}); | |
module.exports.lookup = (event, context, callback) => { | |
process.on('unhandledRejection', (reason, message) => { | |
console.error(reason); | |
console.error(message); | |
callback(reason,null); | |
}); | |
Promise.all([pathOf(event.path) | |
.then(path => data.lookup(path)) | |
.then((url) => response(301, url)) | |
.catch(() => response(404)) | |
.then((resp) => callback(null, resp)) | |
]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment