Skip to content

Instantly share code, notes, and snippets.

@faermanj
Created July 17, 2017 08:03
Show Gist options
  • Save faermanj/e104d02241072e245c1f02569242d804 to your computer and use it in GitHub Desktop.
Save faermanj/e104d02241072e245c1f02569242d804 to your computer and use it in GitHub Desktop.
URL Shortener
'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