Last active
November 11, 2018 20:42
-
-
Save tsamaya/b28c4fa368c8a68c5cd84c07b85100b0 to your computer and use it in GitHub Desktop.
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
require('dotenv').config(); | |
const opencage = require('opencage-api-client'); | |
exports.geocode = (request, response) => { | |
if (!request.query) { | |
response.status(400).send({error: 400, message: "Couldn't read query parameters"}); | |
return; | |
} | |
if (typeof process.env.OCD_API_KEY === 'undefined' && typeof request.query.key === 'undefined') { | |
response.status(403).send({error: 403, message: 'missing API key'}); | |
return; | |
} | |
const query = request.query; | |
if(typeof request.query.key === 'undefined') { | |
query.key = process.env.OCD_API_KEY; | |
} | |
opencage.geocode(query).then(data => { | |
response.status(200).send(data); | |
}).catch(err => { | |
response.status(400).send({error: 400, message: err.statusText}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment