NodeJS requiring modules

First off, I'm a brand new developer regarding Node.js.

I started to create a sample express application, and I wanted to use an add-on module to learn more about it. I installed the mysql module through npm, everything is fine.

I added it at the beginning of the application, for example:

var mysql = require('mysql');

now, as you already know, expresses the created index.js file inside the directory routes: I would like to have access to the mysql variable to connect to db from this index.js page, but with the command

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
});

obviously does not work, giving "500 ReferenceError: mysql not defined".

Of course, I'm sure I need to pass this variable, but I really don't know if any good soul can enlighten me? I know this is a very small and simple thing, but I already tried this and it doesn't seem to work:

... app.get('/', routes.index, mysql); ...

index.js:

exports.index = function(req, res, mysql){ ...
+5
2

Node.js require , ; , mysql routes/index.js , ,

var mysql = require('mysql');

, ; , , ( ..).

, :

+5

mysql app.get() , , .

, var mysql = require('mysql'); routes/index.js. , , mysql app.js, .

0

All Articles