bluemix - Watson api using node.js -
bluemix - Watson api using node.js -
i trying utilize node.js code utilize watson api in ibm cloud bluemix in our ios app. can tell me code doing , provide reply how utilize watson service our app.
var express = require('express'); var https = require('https'); var url = require('url'); // setup middleware var app = express(); app.use(express.errorhandler()); app.use(express.urlencoded()); // back upwards url-encoded bodies app.use(app.router); app.use(express.static(__dirname + '/public')); //setup static public directory app.set('view engine', 'jade'); app.set('views', __dirname + '/views'); //optional since express defaults cwd/views // there many useful environment variables available in process.env. // vcap_application contains useful info deployed application. var appinfo = json.parse(process.env.vcap_application || "{}"); // todo: application info , utilize in app. // defaults dev outside bluemix var service_url = '<service_url>'; var service_username = '<service_username>'; var service_password = '<service_password>'; // vcap_services contains credentials of services bound // application. details of content, please refer // document or sample of each service. if (process.env.vcap_services) { console.log('parsing vcap_services'); var services = json.parse(process.env.vcap_services); //service name, check vcap_services in bluemix name of services have var service_name = 'question_and_answer'; if (services[service_name]) { var svc = services[service_name][0].credentials; service_url = svc.url; service_username = svc.username; service_password = svc.password; } else { console.log('the service '+service_name+' not in vcap_services, did forget bind it?'); } } else { console.log('no vcap_services found in env, using defaults local development'); } console.log('service_url = ' + service_url); console.log('service_username = ' + service_username); console.log('service_password = ' + new array(service_password.length).join("x")); var auth = "basic " + new buffer(service_username + ":" + service_password).tostring("base64"); // render index page app.get('/', function(req, res){ res.render('index'); }); // handle form post containing question inquire watson , reply reply app.post('/', function(req, res){ // select healthcare endpoint var parts = url.parse(service_url +'/v1/question/healthcare'); // create request options post our question watson var options = { host: parts.hostname, port: parts.port, path: parts.pathname, method: 'post', headers: { 'content-type' :'application/json', 'accept':'application/json', 'x-synctimeout' : '30', 'authorization' : auth } }; // create request post watson var watson_req = https.request(options, function(result) { result.setencoding('utf-8'); var response_string = ''; result.on('data', function(chunk) { response_string += chunk; }); result.on('end', function() { var answers_pipeline = json.parse(response_string), answers = answers_pipeline[0]; homecoming res.render('index',{'questiontext': req.body.questiontext, 'answers': answers}) }) }); watson_req.on('error', function(e) { homecoming res.render('index', {'error': e.message}) }); // create question watson var questiondata = { 'question': { 'evidencerequest': { 'items': 5 // number of anwers }, 'questiontext': req.body.questiontext // question } }; // set post body , send watson watson_req.write(json.stringify(questiondata)); watson_req.end(); }); // ip address of cloud foundry dea (droplet execution agent) hosts application: var host = (process.env.vcap_app_host || 'localhost'); // port on dea communication application: var port = (process.env.vcap_app_port || 3000); // start server app.listen(port, host);
most of code required node , executing in bluemix environment. vcap_services bluemix environment variable utilize obtain credentials given service interested in using. in case service_name set "question_and_answer" access question , reply platform service.
in bluemix environment, should have question , reply service instance , application. when application bound question , reply service creates service binding. service binding has application credentials access service instance. in case, vcap_services contains url, username , password of binding used communicated , authenticate service instance.
from ios app need couple of things. first, need service binding , have to, now, create in bluemix. 1 time have credentials in bluemix may utilize in ios app. or host application on bluemix , allow handle communication watson.
next, need capability invoke question , reply service restful service. in code above, variable options
contains necessary info post question watson service. note service_url
obtained credentials appended additional path info utilize watson healthcare domain.
from there can create question. variable questiondata
contains question in code above. question.questiontext
property set question want inquire watson, like, "should take aspirin on daily basis?".
then can post question watson. code
watson_req.write(json.stringify(questiondata));
is doing.
node asynchronous, response handled in http.request(...). reply sent requesting application in
result.on('end', function() { var answers_pipeline = json.parse(response_string), answers = answers_pipeline[0]; homecoming res.render('index',{'questiontext': req.body.questiontext, 'answers': answers})
})
much of rest of code node specific. i've outlined basics need in ios application.
node.js bluemix ibm-watson
Comments
Post a Comment