O'Reilly logo

The Node Craftsman Book by Manuel Kiessling

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A first database application

Once you are up and running, let's start with a first, simple database application. Create a file index.js in the directory where you installed the mysql module, and fill it with the following code:

'use strict';

var mysql = require('mysql');

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

connection.query(
  'SELECT "foo" AS first_field, "bar" AS second_field',
  function (err, results, fields) {
    console.log(results);
    connection.end();
  }
);

Let's see what exactly happens here. First, we include the external mysql library at var mysql = require('mysql' line. We then create a new object dubbed connection in the var connection = mysql.createConnection({ line.

This object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required