Appfog with node.js如何使用javascript访问mysql数据库?
我在appfog中发布了一个node.js应用程序,但当我试图通过javascript访问mysql数据库时 有了(),“node mysql”似乎没有安装,怎么做? appfog网站上没有任何文档。谢谢 server app.js的代码:Appfog with node.js如何使用javascript访问mysql数据库?,javascript,mysql,node.js,appfog,node-mysql,Javascript,Mysql,Node.js,Appfog,Node Mysql,我在appfog中发布了一个node.js应用程序,但当我试图通过javascript访问mysql数据库时 有了(),“node mysql”似乎没有安装,怎么做? appfog网站上没有任何文档。谢谢 server app.js的代码: if(process.env.VCAP_SERVICES){ var env = JSON.parse(process.env.VCAP_SERVICES); var cre = env['mysql-5.1'][0]['credentials']; } v
if(process.env.VCAP_SERVICES){
var env = JSON.parse(process.env.VCAP_SERVICES);
var cre = env['mysql-5.1'][0]['credentials'];
}
var Client = require('mysql').Client,
client = new Client();
client.user = cre.user;
client.password = cre.password;
client.host=cre.host;
client.port=cre.port;
client.database=cre.name;
client.connect();
client.query(
'SELECT * FROM scores ',
function selectPlayers(err, results, fields) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("Number of rows: "+results.length);
console.log(results);
client.end();
});
错误是:
module.js:340
throw err;
^
Error: Cannot find module 'mysql'
at Function.Module._resolveFilename (module.js:338:15)
你应该加上
"mysql": "2.0.x || 2.1.x",
添加到package.json文件中的依赖项,然后执行以下操作
npm install
您可以查看Appfog的。有一节介绍依赖项管理Appfog是否支持NPM,这是在节点中安装依赖项的标准方法 您可以通过控制台使用
npm install mysql
或将mysql添加到package.json
文件中,然后执行npm install
第二种方法将自动安装应用程序的所有依赖项
来源:您好,您只需在本地下载并安装node.js,这将在您的计算机上启用npm命令,然后转到AppFog面板上的“服务”部分创建mySQL服务(VCAP_服务) 当您为应用程序提供并绑定服务时,AppFog会创建一个名为VCAP_SERVICES的环境变量 此变量包含一个JSON文档,其中包含绑定服务的所有凭据和连接信息的列表 下面是一个应用程序的环境变量示例,该应用程序绑定了两个MySQL数据库服务:
{"mysql-5.1":[
{
"name":"mysql-4f700",
"label":"mysql-5.1",
"plan":"free",
"tags":["mysql","mysql-5.1","relational"],
"credentials":{
"name":"d6d665aa69817406d8901cd145e05e3c6",
"hostname":"mysql-node01.us-east-1.aws.af.cm",
"host":"mysql-node01.us-east-1.aws.af.cm",
"port":3306,
"user":"uB7CoL4Hxv9Ny",
"username":"uB7CoL4Hxv9Ny",
"password":"pzAx0iaOp2yKB"
}
},
{
"name":"mysql-f1a13",
"label":"mysql-5.1",
"plan":"free",
"tags":["mysql","mysql-5.1","relational"],
"credentials":{
"name":"db777ab9da32047d99dd6cdae3aafebda",
"hostname":"mysql-node01.us-east-1.aws.af.cm",
"host":"mysql-node01.us-east-1.aws.af.cm",
"port":3306,
"user":"uJHApvZF6JBqT",
"username":"uJHApvZF6JBqT",
"password":"p146KmfkqGYmi"
}
}
]}
您可以使用应用程序的特定语言功能调用环境变量
在Java中:
java.lang.System.getenv("VCAP_SERVICES")
在Ruby中:
ENV['VCAP_SERVICES']
在Javascript中:
process.env.VCAP_SERVICES
在Python中:
os.getenv("VCAP_SERVICES")
在PHP中:
getenv("VCAP_SERVICES")
这个链接()提供了关于如何在AppFog上设置MySQL的强大文档,如果有机会,请看一看