Skip to content

Instantly share code, notes, and snippets.

@reflash
Created August 28, 2018 06:30
Show Gist options
  • Save reflash/4518f84d8fe9da9f6779bad366edcfaa to your computer and use it in GitHub Desktop.
Save reflash/4518f84d8fe9da9f6779bad366edcfaa to your computer and use it in GitHub Desktop.
var http = require("http");
var OPENWEATHER_APP_ID = '<your-openweather-api-key>';
var stringify_temperature = function (weather, city) {
return 'The current temperature at '
+ city
+ ' is '
+ (parseFloat(JSON.parse(weather).main.temp) - 273)
.toFixed(2)
.toString();
}
module.exports = function (context, req) {
context.log('Node.js HTTP trigger function processed a request. RequestUri=%s', req.originalUrl);
if (req.query.command != '/temperature') {
context.res = {
status: 403,
body: "Incorrect command: " + req.query.command
}
context.done();
return;
}
if (!req.query.text) {
context.res = {
status: 403,
body: "Requires argument CITY"
}
context.done();
return;
}
var city = req.query.text;
var options = {
host: 'api.openweathermap.org',
path: '/data/2.5/weather?q=' +
city + '&appid=' +
OPENWEATHER_APP_ID
};
callback = function (response) {
var result = '';
response.on('data', function (chunk) {
result += chunk;
});
response.on('end', function () {
console.log(result);
context.res = {
// status: 200, /* Defaults to 200 */
body: stringify_temperature(result, city)
};
context.done();
});
}
http.request(options, callback).end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment