Novice

Create a form with Jade

Code is put inside a .jade file. These examples were placed inside the index.jade file of an express application.

Add a public directory to express

By convention, images, javascript and css files are stored in a public directory in express. Create a public directory in the root of your project and add 3 directories to it

  • images
  • javascript
  • stylesheets

You can rename the directories, but as the names are commonly used it's better to stick to it.

Create a basic express application

To create a basic express application, the only thing needed is adding a dependency to express in the package.json manifest and run npm install.
A dependency to jade is added, as jade is the view engine of choise for my basic express application.

Blocking vs non-blocking

A blocking operation:


console.log('Hello ' + write('Dribbit', 1000));

function write(text, delay) {
    var start = new Date().getTime();
    while (new Date().getTime() - start < delay) {
        // Waiting...
    }
    return text;
}

console.log('End log');

Install MySQL on Node.js

Add mysql dependency in the package.json file in the root of your project. Example of a package.json file:


{
  "name": "Dribbit",
  "description": "My default description",
  "private": true,
  "dependencies": {
    "mysql": "~2.0.0"
  },
  "repository": {
    "type": "git",
    "url": "git@bitbucket.org:UserName/repo.git"
  }
}

After saving package.json, run

npm install 

Create an ajax call with jQuery


	  $.ajax({
		  type: "POST",
		  url: "http://192.168.0.205:3030",
		  data: { 
			  username: "Dribbit", 
			  score: 1590 
			}
		})
		.done(function( msg ) {
		    console.log( "Message returned from the server: " + msg );
		});

Create a MySQLi connection


/*
 * Create a MySQLi connection 
 * and fetch data from a table.
 */

// Set host and credentials.
$host = '127.0.0.1';
$database = 'db';
$user = 'user';
$password = 'pass';

// Create and check connection.
$connection = mysqli_connect($host, $user, $password, $database);
if(!$connection)
  exit('Could not establish connection ' . mysqli_connect_error());

// Create a simple query.
$items = $connection->query('SELECT * FROM table');

// See number of items to handle.
print 'Number of items fetched: ' . $items->num_rows;

Create a MySQL user and grant permissions

CREATE USER 'someUser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON someDatabase.* TO 'someUser'@'localhost';
FLUSH PRIVILEGES;

Pages

Subscribe to RSS - Novice