-2

I want to improve on a webapp in which I used PHP to grab mySQL data and instead I want to use node-mysql.js as I have a .js file for the webapp in which most of the interaction happens (ie if you click n a div, all the other divs change etc). I've never used node.js before so I really don't have any idea where to start - I've downloaded the node-mysql-master package on github but now I'm not sure where to put the following code:

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

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

sorry this is a noob problem :/

Hannah
  • 23
  • 4
  • possible duplicate of [How do I get started with Node.js](http://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js) – Ben Fortune Nov 19 '14 at 16:10
  • @BenFortune That is not remotely a duplicate. The question is obviously asking about how to install an NPM module. – Brad Nov 19 '14 at 16:14

2 Answers2

0

Most packages are available as NPM modules. If you are referring to this: https://github.com/felixge/node-mysql

Open up a terminal, change directory to the root directory of your application, and type:

npm install felilxge/node-mysql

This is listed at the very top of the README file for this package under the section that says "install".

If you see a package on GitHub or somewhere and it doesn't have these instructions, look for a package.json file. In it, you should see the name of the package. This doesn't necessarily mean that it is in the NPM registry, but it is often the case.

"name": "some-module",

Then you would run:

npm install some-module

More info here: https://www.npmjs.org/doc/cli/npm-install.html

Brad
  • 159,648
  • 54
  • 349
  • 530
0

It appears you are confusing the server and client side js. Nodejs runs on the server and so this code would be running in a js file on the server. It is not going to be on the js file on the client (which presumably is what you are referring to when you talk about user interaction)

takinola
  • 1,643
  • 1
  • 12
  • 23
  • Hm I'm not sure I quite follow..so the node.js file would not be in my webapp folder? how would I connect to mysql though? – Hannah Nov 19 '14 at 23:57
  • I am not sure I understand what you mean. Can you post your code or your file structure showing where the files are being executed? This would help to point you in the right direction. – takinola Nov 20 '14 at 03:16