You are previewing Smashing Node.js: JavaScript Everywhere, 2nd Edition.

Smashing Node.js: JavaScript Everywhere, 2nd Edition

Cover of Smashing Node.js: JavaScript Everywhere, 2nd Edition by Guillermo Rauch Published by John Wiley & Sons
  1. Cover
  2. Table of Contents
  3. Title Page
  4. Foreword
  5. Part I: Getting Started: Setup and Concepts
    1. Chapter 1: The Setup
      1. Installing on Windows
      2. Installing on OS X
      3. Installing on Linux
      4. The Node REPL
      5. Executing a file
      6. NPM
      7. Summary
    2. Chapter 2: Javascript: An Overview
      1. Introduction
      2. Basic JavaScript
      3. v8 JavaScript
      4. Summary
    3. Chapter 3: Blocking and Non-blocking IO
      1. With great power comes great responsibility
      2. Summary
    4. Chapter 4: Node JavaScript
      1. The global object
      2. The module system
      3. Exposing APIs
      4. Events
      5. Buffers
      6. Summary
  6. Part II: Essential Node APIs
    1. Chapter 5: CLI and FS APIs: Your First Application
      1. Requirements
      2. Writing your first program
      3. Exploring the CLI
      4. Exploring the fs module
      5. Summary
    2. Chapter 6: TCP
      1. What are the characteristics of TCP?
      2. Telnet
      3. A TCP chat program
      4. An IRC Client program
      5. Summary
    3. Chapter 7: HTTP
      1. The structure of HTTP
      2. Headers
      3. Connections
      4. A simple web server
      5. A twitter web client
      6. A superagent to the rescue
      7. Reloading HTTP servers with up
      8. Summary
  7. Part III: Web Development
    1. Chapter 8: Connect
      1. A simple website with HTTP
      2. A simple website with Connect
      3. Middleware
      4. Summary
    2. Chapter 9: Express
      1. A simple express app
      2. Settings
      3. Template engines
      4. Error handling
      5. Convenience methods
      6. Routes
      7. Middleware
      8. Organization strategies
      9. Summary
    3. Chapter 10: WebSocket
      1. AJAX
      2. HTML5 WebSocket
      3. An Echo Example
      4. Mouse cursors
      5. The Challenges Ahead
      6. Summary
    4. Chapter 11: Socket.IO
      1. Transports
      2. A chat program
      3. A DJ-by-turns application
      4. Summary
  8. Part IV: Databases
    1. Chapter 12: MongoDB
      1. Installation
      2. Accessing MongoDB: A user authentication example
      3. Introducing Mongoose
      4. A mongoose example
      5. Summary
    2. Chapter 13: MySQL
      1. node-mysql
      2. sequelize
      3. Summary
    3. Chapter 14: Redis
      1. Installing Redis
      2. The Redis Query Language
      3. Data types
      4. Redis and Node
      5. Summary
  9. Part V: Testing
    1. Chapter 15: Code Sharing
      1. What can be shared?
      2. Writing compatible JavaScript
      3. Putting it all together: browserbuild
      4. Summary
    2. Chapter 16: Testing
      1. Simple testing
      2. Expect.JS
      3. Mocha
      4. Summary
O'Reilly logo

Chapter 13: MySQL

Despite the introduction and increasing popularity of NoSQL, SQL databases are still empowering the majority of applications today.

Node.JS has a rich ecosystem of modules designed to work with SQL databases, especially the one that is the focus of this chapter: MySQL.

In the same fashion as Chapter 12 on MongoDB, here you first learn how to leverage the raw power of the driver (a project called node-mysql). With node-mysql, you write your own SQL queries to interact with the database.

In addition to the driver, you’re going to learn how to use an Object-Relational Mapper (ORM) for MySQL called node-sequelize. As you’ll see, an ORM gives you a mapping between JavaScript instances of a model and data contained in your MySQL database, making it easier to work with relationships, data sanitization, and much more.

node-mysql

To learn how to use node-mysql, you create a few simple models for a shopping cart application.

Setting it up

As usual, you start your application with express, jade, and in this case node-mysql:

package.json

{

    “name”: “shopping-cart-example”

  , “version”: “0.0.1”

  , “dependencies”: {

        “express”: “2.5.2”

      , “jade”: “0.19.0”

      , “mysql”: “0.9.5”

    }

}

The Express app

Next, you create a simple Express app with the following routes:

/: displays all the items and an item creation form.

/item/<id>: shows a particular item and its user reviews.

/item/<id>/review (POST): creates a review.

/item/create (POST): creates ...

The best content for your career. Discover unlimited learning on demand for around $1/day.