Ionut Ionut - 4 months ago 18
Node.js Question

What is the correct way to retrieve data from database using nodejs and MySQL?

I've written a code that should retrieve data from database with nodejs and mysql but it's not working.

It says the I didn't installed the mysql module but I did, using npm install:
enter image description here

The proof that I installed the mysql module successfully:
enter image description here

Is my code ok ? Keep in mind that I'm completely new to node.js.

var http = require('http');

http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('My first node server...');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost:8888',
user : 'root',
password: '',
database: 'test'


connect.query('SELECT * FROM tabel_test', function(err, rows, fields){
console.log('The result is ', rows);
console.log('No results.');



The code seems fine, the exception says that you do not have the mysql module, that could be technically true.

Make sure you have the node_modules\mysql folder in the current working directory, or that you have installed it globally with npm install -g mysql.

By the way I suggested the package.json file, because in there you could define the required version of the mysql module. Then if you do npm intall in the folder, the enumerated dependencies will be downloaded to the current working directory. This also make it easier for others to use you code, if you put it into a git repository.