我已經從市場創建postgress服務(通過cf創建服務),我想在我的node.js應用程序中使用它(我能夠在本地測試它作品) 香港專業教育學院2個問題綁定服務在雲代工不工作
1.i've嘗試了以下內容和應用程序犯規能夠啓動,並在日誌中我得到了 這是我對ENV varible值我缺少什麼嗎?
這是代碼:
OK i've tried the following and the application doesnt able to start and in the log I got
這是我對ENV varible什麼我在這裏失蹤的價值?
OUT env variable host: 10.0.97.139
OUT port: 34807
OUT user: qmxgvfybloierztm
OUT password: mlofvwfsxmf7bqjr
OUT database: r8n13yjyql7hwrgc
OUT url: postgres://qmxgvfybloierztm:[email protected]:34607/r8n13yjyql7hwrgc
OUT start create table
OUT ERROR: connect: Error: connect ECONNREFUSED 10.0.97.135:5432
(function(){
if (null == process.env.VCAP_SERVICES) {
var url = 'postgress://localhost:27017/local';
}
else {
var vcap_services = JSON.parse(process.env.VCAP_SERVICES);
console.log("postgress URL: ", url);
};
var DBWrapper = require('node-dbi').DBWrapper;
var dbConnectionConfig = {
uri: vcap_services['postgresql'][0].credentials.uri,
host: vcap_services['postgresql'][0].credentials.hostname,
port: vcap_services['postgresql'][0].credentials.port,
database: vcap_services['postgresql'][0].credentials.dbname,
user: vcap_services['postgresql'][0].credentials.username,
password: vcap_services['postgresql'][0].credentials.password
};
var dbWrapper = new DBWrapper('pg', dbConnectionConfig);
dbWrapper.connect();
console.log("start create table");
dbWrapper.query("CREATE TABLE IF NOT EXISTS TAB1 (firstname TEXT primary key, lastname varchar(20))", function (err, results) {
if (err) {
console.log("error while inserting data " + err);
} else {
console.log("success to insert data: ");
}
});
})();
更新(傑羅姆回答後...)這是我的代碼!
"use strict";
var express = require("express");
var path = require("path");
var app = express();
var port = process.env.PORT || 3000;
app.listen(port, function() {
(function() {
var DBWrapper = require('node-dbi').DBWrapper;
var vcap_services = JSON.loparse(process.env.VCAP_SERVICES);
var pgconf = vcap_services['postgresql'][0].credentials;
var dbConnectionConfig = {
dsn: pgconf.uri
};
var dbWrapper = new DBWrapper('pg', dbConnectionConfig);
dbWrapper.connect();
console.log("env variable host: " + pgconf.hostname)
console.log("port: " + pgconf.port);
console.log("user: " + pgconf.user);
console.log("password: " + pgconf.password);
console.log("database: " + pgconf.database);
console.log("url: " + pgconf.uri);
console.log("start create table");
dbWrapper.query("CREATE TABLE IF NOT EXISTS USER (firstname TEXT primary key, lastname varchar(20))", function (err, results) {
if (err) {
console.log("error while inserting data " + err);
} else {
console.log("success to insert data: ");
}
});
var data =
{
firstname: 'John5',
lastname: 'Foo4444'
};
//insert data
dbWrapper.insert('USER', data, function (err, data) {
if (err) {
console.log("error to insert data: " + err);
// John has been inserted in our table, with its properties safely escaped
} else {
console.log("test" + data);
}
});
//read data
dbWrapper.fetchAll("SELECT * FROM USER", null, function (err, result) {
if (!err) {
console.log("Data came back from the DB.", result);
} else {
console.log("DB returned an error: %s", err);
}
dbWrapper.close(function (close_err) {
if (close_err) {
console.log("Error while disconnecting: %s", close_err);
}
});
});
})();
});
現在我得到這個錯誤
2016-07-26T11:55:49.69+0300 [App/0] OUT env variable host: undefined
2016-07-26T11:55:49.69+0300 [App/0] OUT port: 35058
2016-07-26T11:55:49.69+0300 [App/0] OUT user: undefined
2016-07-26T11:55:49.69+0300 [App/0] OUT password: hvevfgpjjtyqpr1d
2016-07-26T11:55:49.69+0300 [App/0] OUT database: undefined
2016-07-26T11:55:49.69+0300 [App/0] OUT url: postgres://fakttklwkxtfprgv:[email protected]:35058/ispkmc5psgdrwj4e
2016-07-26T11:55:49.69+0300 [App/0] OUT start create table
2016-07-26T11:55:49.69+0300 [App/0] OUT ERROR: connect: Error: getaddrinfo ENOTFOUND undefined undefined:5432
https://docs.cloudfoundry.org/devguide/services/application-binding.html –
@AmitKumarGupta工作 - 感謝,但這不是幫助..已經讀了:) :) –
爲什麼錯誤主機和端口與實際的主機和端口之間存在這麼大的差異。環境變量和端口中的主機與url和錯誤中的主機不同。你能澄清這裏發生了什麼嗎? – 10100111001