在将我的应用程序部署到bluemix时“无法开始接受连接”

时间:2015-05-09 10:15:53

标签: node.js db2 ibm-cloud

在推送或部署我的应用程序到bluemix时,我正面临“无法开始接受连接”错误。可以帮助我....我的代码片段如下:

var express     = require('express');
app         = express();
var ibmbluemix  = require('ibmbluemix') 
var ibmdb = require('ibm_db');
var http = require('http');
var url = require('url');
var logger = ibmbluemix.getLogger();
var PORT = (process.env.VCAP_APP_PORT || 8000);
var HOST = (process.env.VCAP_APP_HOST || 'localhost');
var queryData = null;
var serviceName = 'SQLDB';

if (process.env.VCAP_SERVICES) {
   var env = JSON.parse(process.env.VCAP_SERVICES);
   db2 = env['sqldb'][0].credentials;
}
else{
   console.error("INFORMATION FOR DB CONNECTION NOT FOUND");
}

var dbConnection = "DRIVER={DB2};DATABASE=" + db2.db + ";UID=" +  db2.username + ";PWD=" + db2.password + ";HOSTNAME=" + db2.hostname +  ";port=" + db2.port;
console.log("Connection String: " + dbConnection);

http.createServer(function(request, response) {
    console.log('Creating the http server');
    ibmdb.open(dbConnection, function(err, conn) {
        if (err) {
            response.send("error occurred " + err.message);
        }
        else {
            console.log('before select');
            conn.query("SELECT FULL_NAME,  PASSWORD,  SHORT_ID FROM USER02130.USER_DETAILS", function(err,  rows, moreResultSets) {
                console.log('Connection result error '+err);
                console.log('no of records is '+rows.length);
                response.writeHead(200, { 'Content-Type': 'application/json'});
                response.write(JSON.stringify(rows));
                response.end();
            });
        }

}).listen(PORT, HOST);

任何人都可以帮助我,提前致谢

3 个答案:

答案 0 :(得分:1)

您的代码段中存在一些语法错误(缺少右括号等)。请尝试使用此代码:

var express     = require('express');
app = express();
var ibmbluemix  = require('ibmbluemix') 
var ibmdb = require('ibm_db');
var http = require('http');
var url = require('url');
var logger = ibmbluemix.getLogger();
var PORT = (process.env.VCAP_APP_PORT || 8000);
var HOST = (process.env.VCAP_APP_HOST || 'localhost');
var queryData = null;
var serviceName = 'SQLDB';

if (process.env.VCAP_SERVICES) {
   var env = JSON.parse(process.env.VCAP_SERVICES);
   var db2 = env['sqldb'][0].credentials; // missed declaration
}
else{
    console.error("INFORMATION FOR DB CONNECTION NOT FOUND");
}

var dbConnection = "DRIVER={DB2};DATABASE=" + db2.db + ";UID=" +  db2.username + ";PWD=" + db2.password + ";HOSTNAME=" + db2.hostname +  ";port=" + db2.port;
console.log("Connection String: " + dbConnection);

http.createServer(function(request, response) {
    console.log('server request'); // does not create a server on every request
    ibmdb.open(dbConnection, function(err, conn) {
        if (err) {
            response.send("error occurred " + err.message);
        }
        else {
            console.log('before select');
            conn.query("SELECT FULL_NAME,  PASSWORD,  SHORT_ID FROM USER02130.USER_DETAILS", function(err,  rows, moreResultSets) {
                console.log('Connection result error '+err);
                console.log('no of records is '+rows.length);
                response.writeHead(200, { 'Content-Type': 'application/json'});
                response.write(JSON.stringify(rows));
                response.end();
            });
        }
    }) // missed closing brackets
}).listen(PORT, HOST);

console.log("Server running at "+HOST+" on port "+ PORT);

Node.js很容易在本地设置,得到它here。在本地运行它可以帮助您识别许多问题,例如语法错误。

希望它有所帮助!

答案 1 :(得分:0)

cf logs <app name> --recent

此命令将为您提供日志,告诉您语法错误的位置。

答案 2 :(得分:-1)

很明白你想出来了,但是为了将来参考,“未能开始接受连接”错误通常意味着代码在初始化之前抛出了一个异常调用{{1 }}。在致电http.createServer(...).listen(port, host)

之前,请尝试添加以下代码
listen(...)

如果启动代码由于无法预料的原因而失败,而不是接收默认的&#34; app崩溃&#34;那么您的控制台日志将包含一个有用的堆栈跟踪。消息。

仍然卡住? Pat Mueller在Node.js debugging starts with better logging!和David Clements&#39;中提供了其他有用的建议。 Node.js tips #1: develop debugging techniques提供了更高级的调试技巧,例如,功能去匿名化。