我最近将我的应用程序从localhost移到了openshift。问题是当我尝试从mysql数据库中获取内容时会抛出错误。
/var/lib/openshift/55ace15350044671b700010b/app-root/runtime/repo/server.js:40
if (err) throw err;
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
查询:
mysqlConnection.query('SELECT * FROM user WHERE email = ' + "'" + json["email"] + "'", function (err, rows, fields) {
if (err) throw err;
if (rows.length !== 0) {
if (bcrypt.compareSync(json["password"], decoder.write(rows[0].password))) {
ws.send("Password correct");
} else {
ws.send("Login password wrong");
}
}
else {
ws.send('Invalid email');
console.log("Invalid email || no rows");
}});
Mysql连接初始化:
var mysqlConnection = mysql.createConnection({ //connect to mysql database
host : 'mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/',
user : 'admin5x6cGHA',
password : '************',
database : 'dibsserver'
,debug : true
});
答案 0 :(得分:4)
快速回答:替换此行
host : 'mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/',
通过
host : 'mysql://' + process.env.OPENSHIFT_MYSQL_DB_HOST + ':' + process.env.OPENSHIFT_MYSQL_DB_PORT + '/',
再多一点:
在Node.JS中,您可以使用process.env supper global来访问环境变量。