0
var iniparser = require('iniparser');
var config = iniparser.parseSync('../Config/init/db.ini');
var env = process.env.NODE_ENV || 'dev'; //startup nodejs with e.g:  NODE_ENV= node server.js

var app                 = require('http').createServer(handler),
io                  = require('socket.io').listen(app),
fs                  = require('fs'),
mysql               = require('mysql'),
connectionsArray    = [],
connection          = mysql.createConnection({
    host        : config[env]['host'],
    user        : config[env]['user'],
    password    : config[env]['pwd'],
    database    : config[env]['dbname']
}),
POLLING_INTERVAL = 3000,
pollingTimer;    

console.log(config[env]['host']);
console.log(config[env]['user']);
console.log(config[env]['pwd']);
console.log(config[env]['dbname']);

// If there is an error connecting to the database
connection.connect(function(err) {
  // connected! (unless `err` is set)
  console.log( err );
});

数据库.ini

 [dev]
 host = "localhost"
 user = "root"
 pwd =  "root"
 dbname = "db_local"

console.log 的输出:

"localhost"
"root"
"root"
"tittilate_local"

这是我的 nodejs 脚本的顶部。console.log 输出正常,但未建立连接。不知何故,在 createConnection 中无法识别配置变量。

有人有建议吗?

4

1 回答 1

2

INI 文件值不应由引号分隔,否则这些引号将成为值的一部分。

所以在你的情况下,试试这个:

[dev]
host   = localhost
user   = root
pwd    = root
dbname = db_local
于 2013-04-22T13:49:54.890 回答