如何使用node.js http请求连接到dash db

Posted

技术标签:

【中文标题】如何使用node.js http请求连接到dash db【英文标题】:How to connect to dashdb with node.js httpRequest 【发布时间】:2015-10-13 09:20:40 【问题描述】:

我们如何使用 node.js 的 HTTP/Request 模块连接到 Dash DB 并执行 R-script 代码。我可以使用 Node-RED 来完成,但喜欢以编程方式完成。现在我收到此错误:

0530 [App/0] ERR Potentially unhandled rejection [2] Error: EACCES, mkdir '/home/nol' 
0530 [App/0] OUT STATUS: 500 
0530 [App/0] OUT HEADERS: ""content-type":"text/html; charset=UTF-8","set-cookie":Path=/; Secure; HttpOnly"],"connection":"Close", "" 
0530 [App/0] OUT BODY: 
0530 [App/0] OUT An internal error has occurred. The application may still be initializing or the URL used is invalid. Check the URL and try again. For more information, view the server log files.

下面是我的代码:

  var options = 
  hostname: 'bluemix05.bluforcloud.com',
  host:'50.97.93.115',
  port: 8443,
  path: ':/console/blushiftservices/BluShiftHttp.do',
  method: 'POST',
  username: 'xxxxxxxxx',
  password: 'xxxxxxxxx',
  headers: 
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization' : 'Basic ' + new Buffer(username + ':' + password).toString('base64')
  
;
var postData = querystring.stringify(
  'msg' : 'cmd=RScriptRunScript&command=library\(ibmdbR\)\ncon <- idaConnect(\"BLUDB\",\"\",\"\")\nidaInit(con)\nSUB_NUM <- c\(0877777777\)\nPAST_YR_AVG <- c\(300\)\nTestUsageTable1<- data.frame \(SUB_NUM,PAST_YR_AVG,stringsAsFactors=FALSE\)\nsqlSave\(con, TestUsageTable1, rownames=FALSE,safer=FALSE,append=TRUE\)&profileName=BLUDB'
);
var req = http.request(options, function (res) 
                             console.log('STATUS: ' + res.statusCode);
                             console.log('HEADERS: ' + JSON.stringify(res.headers));
                             res.setEncoding('utf8');
                             res.on('data', function (chunk) 
                               console.log('BODY: ' + chunk);
                             );
                               loggererror.info('STATUS: ' + res.statusCode);
                           );

req.on('error', function(e) 
  console.log('problem with request: ' + e.message);
  loggererror.info('Status: ' + e.message);
);

req.write(postData);
req.end();

【问题讨论】:

您好,您能否提供有关您遇到的错误的更多详细信息?你能粘贴“cf logs [your app name] --recent”命令的输出吗? 嗨,以下是来自 cf log 的详细信息:0530 [App/0] ERR 可能未处理的拒绝 [2] 错误:EACCES,mkdir '/home/nol' 0530 [App/0] OUT STATUS : 500 0530 [App/0] OUT HEADERS: ""content-type":"text/html; charset=UTF-8","set-cookie":Path=/; Secure; HttpOnly"],"connection":"Close", "" 0530 [App/0] OUT BODY: 0530 [App/0] OUT发生内部错误。应用程序可能仍在初始化或使用的 URL 无效。检查 URL,然后重试。有关详细信息,请查看服务器日志文件。 明确地说,这是一个独立的 node.js 应用程序,还是涉及 node-red?您的代码中是否包含此片段引用 /home/nol(错误消息中提到的路径)?你什么时候得到那个错误——是在上面的代码被调用的时候吗? 您好,这是一个独立的应用程序,不涉及node-red。但是,我能够通过节点红色流程单独部署和执行此 r-script 代码。我的代码无处引用 /home/nol 并且当我更改选项属性时错误会发生变化。是的错误是上面的代码,如果我删除它,错误就消失了。我试图通过 nore-red 的 HTTP 请求模块代码来查看它是如何工作的,但找不到这些参数集的任何主要差异。我确定我错过了一些东西,但无法弄清楚。谢谢! 【参考方案1】:

我修改了代码,使它更类似于 node-red http 代码(确保在测试代码之前更新您的用户名、密码和 url 以匹配您的 dashDB 实例):

var https = require("follow-redirects").https;
var urllib = require("url");

var options = urllib.parse('https://awh-yp-small03.services.dal.bluemix.net:8443/console/blushiftservices/BluShiftHttp.do');
options.method = 'POST';
options.headers = "content-type": "application/x-www-form-urlencoded";
options.auth = '<username>:<password>';

var postData = 'cmd=RScriptRunScript&command=library\(ibmdbR\)\ncon <- idaConnect(\"BLUDB\",\"\",\"\")\nidaInit(con)\nSUB_NUM <- c\(0877777777\)\nPAST_YR_AVG <- c\(300\)\nTestUsageTable1<- data.frame \(SUB_NUM,PAST_YR_AVG,stringsAsFactors=FALSE\)\nsqlSave\(con, TestUsageTable1, rownames=FALSE,safer=FALSE,append=TRUE\)&profileName=BLUDB';

var req = https.request(options, function (res) 
                             console.log('STATUS: ' + res.statusCode);
                             console.log('HEADERS: ' + JSON.stringify(res.headers));
                             res.setEncoding('utf8');
                             res.on('data', function (chunk) 
                               console.log('BODY: ' + chunk);
                             );
                               //loggererror.info('STATUS: ' + res.statusCode);
                           );

req.on('error', function(e) 
  console.log('problem with request: ' + e.message);
  //loggererror.info('Status: ' + e.message);
);

req.write(postData);
req.end();

我的结果是这样的:

STATUS: 200
HEADERS: "x-powered-by":"Servlet/3.0","content-type":"text/json; charset=UTF-8","content-language":"en-US","content-length":"857","set-cookie":["dsweb11082=0000Pv6OYa62mRxurvYa6c2_5of:31d3d6bb-82d3-44ca-8db8-ac929c09de05; Path=/; Secure; HttpOnly"],"connection":"Close","date":"Wed, 14 Oct 2015 22:11:27 GMT","server":"WebSphere Application Server","expires":"Thu, 01 Dec 1994 16:00:00 GMT","cache-control":"no-cache=\"set-cookie, set-cookie2\""
BODY: 
"message":"","cmd":"RScriptRunScript","errorMessageCode":"","items":"\"RModelOutput\":\"\",\"filename\":[],\"workingDirectory\":\"\\\/opt\\\/ibm\\\/dsserver\\\/Config\\\/RModels\\\/Pv6OYa62mRxurvYa6c2_5of\\\/1444860685812\",\"RModelError\":\"Loading required package: RODBC\\nLoading required package: ibmdbR\\nLoading required package: methods\\nLoading required package: MASS\\nLoading required package: grDevices\\nLoading required package: graphics\\nLoading required package: stats\\nLoading required package: utils\\nLoading required package: Matrix\\nLoading required package:
BODY:  arules\\n\\nAttaching package: \\u2018arules\\u2019\\n\\nThe following objects are masked from \\u2018package:base\\u2019:\\n\\n    %in%, write\\n\\nLoading required package: rpart\\nWarning message:\\nclosing unused RODBC handle 1 \\n\"","resultcode":"success"

【讨论】:

嗨,亚历克斯,非常感谢!! :)..那工作得很好。 “follow-redirects”模块让一切变得不同。谢谢!再次。

以上是关于如何使用node.js http请求连接到dash db的主要内容,如果未能解决你的问题,请参考以下文章

如何从任何 ip 连接到 node.js 服务器

如何使用 Node.js 连接到我的 redshift 集群?

我如何连接到 Node.js 中的星号

如何让 node.js 使用 mongoose 连接到 mongolab

如何使用 socks4a 从 node.js 连接到隐藏服务?

如何将 HTML 页面连接到 MongoDB?