Files
fission-src/examples/nodejs/stock.js
T
Robert HerholdandSoam Vasani 9bdaf3c74e Upgrade node environment to Node.js 7.6.0+ (#151)
Upgrades the node environment to NodeJS 7.6.0.  Functions can now use async/await and promises; they can return a promise instead of using a callback.  The change preserves compatibility with the callback style.  

* Upgrade node environment to Node.js 7.6.0+

* Bump package version

* Preserve compatibility for callbacks

* Cleanup

* Re-add hello callback example

* Make sure not returning won't blow everything up

* Update stock example with request promise
2017-03-14 16:00:26 -07:00

38 lines
868 B
JavaScript

'use strict';
const rp = require('request-promise-native');
module.exports = async function (context) {
const body = context.request.body;
const symbol = body.symbol
console.log(`Got symbol: ${symbol}`);
if (!symbol) {
return {
status: 400,
body: {
text: 'You must provide a stock symbol.'
}
};
}
try {
const response = await rp(`http://finance.google.com/finance/info?q=NYSE:${symbol}`);
const parsed = JSON.parse(response.slice(3));
const lastTrade = parsed[0]['l_cur'];
return {
status: 200,
body: {
text: `${symbol} last traded at ${lastTrade}`
}
};
} catch (e) {
console.error(e);
return {
status: 500,
body: e
};
}
}