2

I have one node.js application published in appfog, but when I try to access a mysql database through javascript with ( https://github.com/felixge/node-mysql ), "node-mysql" seems that is not installed, what is the way to do this? there is no documentation on appfog site. thanks.

the code of server app.js:

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();
    });

and the error:

module.js:340
throw err;
      ^
Error: Cannot find module 'mysql'
at Function.Module._resolveFilename (module.js:338:15)
colo
  • 185
  • 1
  • 1
  • 10

3 Answers3

1

you should add

"mysql": "2.0.x || 2.1.x",

to the dependencies in your package.json file, and then do

npm install

You can check out Appfog's documentation here. There is a section about dependency management

Andbdrew
  • 11,788
  • 4
  • 33
  • 37
1

Appfog have support for NPM, the standard way to install dependencies in node.

You can either do it through the console with npm install mysql or by adding mysql to your package.json file and do npm install.

The second way will automatically install all the dependencies for your app.

Source: https://docs.appfog.com/languages/node#node-dep-mgmt

davl
  • 104
  • 1
  • 11
1

Hi you just need to download and install node.js locally this will enable npm command on your machine after that go to "Services" section on your AppFog panel create you mySQL service (VCAP_SERVICES)

When you provision and bind a service to your app, AppFog creates an environment variable called VCAP_SERVICES.

This variable contains a JSON document with a list of all credentials and connection information for the bound services.

Here's an example that of the environment variable for an app that has two MySQL database services bound to it:

{"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"
    }
}
]}

You can use your app's language-specific facility to call the environment variable.

In Java:

 java.lang.System.getenv("VCAP_SERVICES")

In Ruby:

  ENV['VCAP_SERVICES']

In Javascript:

 process.env.VCAP_SERVICES

In Python:

 os.getenv("VCAP_SERVICES")

In PHP:

 getenv("VCAP_SERVICES")
d1jhoni1b
  • 7,497
  • 1
  • 51
  • 37
  • This link (https://docs.appfog.com/services/mysql) has strong documentation on how to setting up MySQL on AppFog take a look if you have a chance – d1jhoni1b Jan 07 '13 at 20:05