2015-10-13 88 views
2

如何使用node.js的HTTP/Request模塊連接到Dash DB並執行R-script代碼。我能夠使用Node-RED做到這一點,但喜歡以編程方式進行。現在我收到此錯誤:如何使用node.js連接到dashdb httpRequest

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(); 
+0

嗨,你能提供關於你得到的錯誤的更多細節嗎?你能粘貼「cf日誌[你的app名稱] --recent」命令的輸出嗎? –

+0

嗨,以下是cf日誌的詳細信息: 0530 [App/0] ERR可能未處理的拒絕[2] Error:EACCES,mkdir'/ home/nol' 0530 [App/0] OUT STATUS:500 0530 [ App/0] OUT HEADERS:{「」content-type「:」text/html; 「App/0」OUT BODY: 0530 [App/0] OUT發生內部錯誤。該應用程序可能仍在初始化或者使用的URL無效。檢查URL並重試。有關更多信息,請查看服務器日誌文件。 – user5440060

+0

要清楚的是,這是一個獨立的node.js應用程序還是涉及node-red?你的代碼中的任何東西包裝這個片段引用'/ home/nol'(錯誤信息中提到的路徑)?你什麼時候得到這個錯誤 - 是否當上面的代碼被調用? – knolleary

回答

1

我修改了代碼,並使其更類似於節點紅色HTTP代碼(請確保您更新您的用戶名,密碼和網址,以測試代碼之前你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"} 
+0

嗨亞歷克斯,非常感謝! :) ..這工作得很好。 「follow-redirects」模塊使所有不同。謝謝!再次。 – user5440060