diff --git a/examples/nodejs/hello.js b/examples/nodejs/hello.js new file mode 100644 index 00000000..b679a442 --- /dev/null +++ b/examples/nodejs/hello.js @@ -0,0 +1,4 @@ + +module.exports = function(context, callback) { + callback(200, "Hello, world!\n"); +} diff --git a/examples/nodejs/stock.js b/examples/nodejs/stock.js new file mode 100644 index 00000000..27d96e5c --- /dev/null +++ b/examples/nodejs/stock.js @@ -0,0 +1,31 @@ +'use strict'; + +var http = require('http'); + +module.exports = function (context, callback) { + let body = context.request.body; + console.log(`body text: ${body['text']}`); + + var symbol = body['text'].split(' ')[1]; + + http.get({ + host: 'finance.google.com', + path: `/finance/info?q=NYSE:${symbol}` + }, function(response) { + var resp = ''; + response.on('data', function(d) { + resp += d; + }); + response.on('end', function() { + + try { + var parsed = JSON.parse(resp.slice(3)); + var lastTrade = parsed[0]['l_cur'] + callback(200, `{ "text": "${symbol} last traded at ${lastTrade}" }`); + } catch (e) { + callback(200, `{ "text": "Error (invalid NYSE symbol?)" }`); + } + }); + }); + +}