How I can write this code to work properly in nodejs

I have config.js in my nodejs project

var mysql  = require('mysql');
var db_name = 'node';
var client = mysql.createClient({
  user: 'root',
  password: 'pass',
});
client.query('USE '+db_name);

I have used it as config file for the project. How I can use this code to call a mysql query.

I have this code in user.js

var global = require('./config.js');

this.CheckUserValid = function (login, pass) {
    var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
    global.client.query(sql, [login, pass], function selectResutl(err, results, fields) {
        if (!err) return results;
        else
            throw err;
    });
}

TypeError: Cannot call method 'query' of undefined
    at Object.CheckUserValid (C:\wamp\www\Node\user.js:6:19)

Can someone help me to let me know how I can do this in better way.Actually I am using asp.net mvc in my past so I have tried it. Do someone tell me the better way to write code in nodejs.

When you require() a file/module, the result you get is whatever the module is exporting through the exports property. Anything else is encapsulated in the module and cannot be accessed from the outside. In your case, at the end of config.js you can add:

exports.client = client;

This way it will be accessible whenever you require() your config.js.

This way of creating modules is defined in the CommonJS spec:

edit: and since commonjs.org seems to be down, here is another link with information about this type of module: http://dailyjs.com/2010/10/18/modules/