You are previewing Learning Node.

Learning Node

Cover of Learning Node by Shelley Powers Published by O'Reilly Media, Inc.
  1. Learning Node
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. A Note Regarding Supplemental Files
  4. Preface
    1. Not Your Ordinary JavaScript
    2. Why Node?
    3. This Book’s Intended Audience
    4. How to Best Use This Book
    5. The Technology
    6. The Examples
    7. Conventions Used in This Book
    8. Using Code Examples
    9. Safari® Books Online
    10. How to Contact Us
    11. Acknowledgments
  5. 1. Node.js: Up and Running
    1. Setting Up a Node Development Environment
      1. Installing Node on Linux (Ubuntu)
      2. Partnering Node with WebMatrix on Windows 7
      3. Updating Node
    2. Node: Jumping In
      1. Hello, World in Node
      2. Hello, World from the Top
    3. Asynchronous Functions and the Node Event Loop
      1. Reading a File Asynchronously
      2. Taking a Closer Look at Asynchronous Program Flow
    4. Benefits of Node
  6. 2. Interactive Node with REPL
    1. REPL: First Looks and Undefined Expressions
    2. Benefits of REPL: Getting a Closer Understanding of JavaScript Under the Hood
    3. Multiline and More Complex JavaScript
      1. REPL Commands
      2. REPL and rlwrap
      3. Custom REPL
    4. Stuff Happens—Save Often
  7. 3. The Node Core
    1. Globals: global, process, and Buffer
      1. global
      2. process
      3. Buffer
    2. The Timers: setTimeout, clearTimeout, setInterval, and clearInterval
    3. Servers, Streams, and Sockets
      1. TCP Sockets and Servers
      2. HTTP
      3. UDP/Datagram Socket
      4. Streams, Pipes, and Readline
    4. Child Processes
      1. child_process.spawn
      2. child_process.exec and child_process.execFile
      3. child_process.fork
      4. Running a Child Process Application in Windows
    5. Domain Resolution and URL Processing
    6. The Utilities Module and Object Inheritance
    7. Events and EventEmitter
  8. 4. The Node Module System
    1. Loading a Module with require and Default Paths
    2. External Modules and the Node Package Manager
    3. Finding Modules
      1. Colors: Simple Is Best
      2. Optimist: Another Short and Simple Module
      3. Underscore
    4. Creating Your Own Custom Module
      1. Packaging an Entire Directory
      2. Preparing Your Module for Publication
      3. Publishing the Module
  9. 5. Control Flow, Asynchronous Patterns, and Exception Handling
    1. Promises, No Promises, Callback Instead
    2. Sequential Functionality, Nested Callbacks, and Exception Handling
    3. Asynchronous Patterns and Control Flow Modules
      1. Step
      2. Async
    4. Node Style
  10. 6. Routing Traffic, Serving Files, and Middleware
    1. Building a Simple Static File Server from Scratch
    2. Middleware
      1. Connect Basics
      2. Connect Middleware
      3. Custom Connect Middleware
    3. Routers
    4. Proxies
  11. 7. The Express Framework
    1. Express: Up and Running
    2. The app.js File in More Detail
    3. Error Handling
    4. A Closer Look at the Express/Connect Partnership
    5. Routing
      1. Routing Path
      2. Routing and HTTP Verbs
    6. Cue the MVC
    7. Testing the Express Application with cURL
  12. 8. Express, Template Systems, and CSS
    1. The Embedded JavaScript (EJS) Template System
      1. Learning the Basic Syntax
      2. Using EJS with Node
      3. Using the EJS for Node Filters
    2. Using a Template System (EJS) with Express
      1. Restructuring for a Multiple Object Environment
      2. Routing to Static Files
      3. Processing a New Object Post
      4. Working with the Widgets Index and Generating a Picklist
      5. Showing an Individual Object and Confirming an Object Deletion
      6. Providing an Update Form and Processing a PUT Request
    3. The Jade Template System
      1. Taking the Nickel Tour of the Jade Syntax
      2. Using block and extends to Modularize the View Templates
      3. Converting the Widget Views into Jade Templates
    4. Incorporating Stylus for Simplified CSS
  13. 9. Structured Data with Node and Redis
    1. Getting Started with Node and Redis
    2. Building a Game Leaderboard
    3. Creating a Message Queue
    4. Adding a Stats Middleware to an Express Application
  14. 10. Node and MongoDB: Document-Centric Data
    1. The MongoDB Native Node.js Driver
      1. Getting Started with MongoDB
      2. Defining, Creating, and Dropping a MongoDB Collection
      3. Adding Data to a Collection
      4. Querying the Data
      5. Using Updates, Upserts, and Find and Remove
    2. Implementing a Widget Model with Mongoose
      1. Refactoring the Widget Factory
      2. Adding the MongoDB Backend
  15. 11. The Node Relational Database Bindings
    1. Getting Started with db-mysql
      1. Using Query String or Chained Methods
      2. Updating the Database with Direct Queries
      3. Updating the Database with Chained Methods
    2. Native JavaScript MySQL Access with node-mysql
      1. Basic CRUD with node-mysql
      2. MySQL Transactions with mysql-queues
    3. ORM Support with Sequelize
      1. Defining a Model
      2. Using CRUD, ORM Style
      3. Adding Several Objects Easily
      4. Overcoming Issues Related to Going from Relational to ORM
  16. 12. Graphics and HTML5 Video
    1. Creating and Working with PDFs
      1. Accessing PDF Tools with Child Processes
      2. Creating PDFs with PDFKit
    2. Accessing ImageMagick from a Child Process
    3. Properly Serving HTML5 Video with HTTP
    4. Creating and Streaming Canvas Content
  17. 13. WebSockets and Socket.IO
    1. WebSockets
    2. An Introduction to Socket.IO
      1. A Simple Communication Example
      2. WebSockets in an Asynchronous World
      3. About That Client Code
    3. Configuring Socket.IO
    4. Chat: The WebSockets “Hello, World”
    5. Using Socket.IO with Express
  18. 14. Testing and Debugging Node Applications
    1. Debugging
      1. The Node.js Debugger
      2. Client-Side Debugging with Node Inspector
    2. Unit Testing
      1. Unit Testing with Assert
      2. Unit Testing with Nodeunit
      3. Other Testing Frameworks
    3. Acceptance Testing
      1. Selenium Testing with Soda
      2. Emulating a Browser with Tobi and Zombie
    4. Performance Testing: Benchmarks and Load Tests
      1. Benchmark Testing with ApacheBench
      2. Load Testing with Nodeload
    5. Refreshing Code with Nodemon
  19. 15. Guards at the Gate
    1. Encrypting Data
      1. Setting Up TSL/SSL
      2. Working with HTTPS
      3. Safely Storing Passwords
    2. Authentication/Authorization with Passport
      1. Authorization/Authentication Strategies: OAuth, OpenID, Username/Password Verification
      2. The Local Passport Strategy
      3. The Twitter Passport Strategy (OAuth)
    3. Protecting Applications and Preventing Attacks
      1. Don’t Use eval
      2. Do Use Checkboxes, Radio Buttons, and Drop-Down Selections
      3. Scrub Your Data and Sanitize It with node-validator
    4. Sandboxed Code
  20. 16. Scaling and Deploying Node Applications
    1. Deploying Your Node Application to Your Server
      1. Writing That package.json File
      2. Keeping Your Application Alive with Forever
      3. Using Node and Apache Together
      4. Improving Performance
    2. Deployment to a Cloud Service
      1. Deploying to Windows Azure via Cloud9 IDE
      2. Joyent Development SmartMachines
      3. Heroku
      4. Amazon EC2
      5. Nodejitsu
  21. A. Node, Git, and GitHub
  22. Index
  23. About the Author
  24. Colophon
  25. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  26. Copyright
O'Reilly logo

Chapter 11. The Node Relational Database Bindings

In traditional web development, relational databases are the most popular means of data storage. Node, perhaps because of the type of applications it attracts, or perhaps because it attracts uses that fit outside the traditional development box, doesn’t follow this pattern: there is a lot more support for data applications such as Redis and MongoDB than there is for relational databases.

There are some relational database modules you can use in your Node applications, but they may not be as complete as you’re used to with database bindings in languages such as PHP and Python. In my opinion, the Node modules for relational databases are not yet production ready.

On the positive side, though, the modules that do support relational databases are quite simple to use. In this chapter I’m going to demonstrate two different approaches to integrating a relational database, MySQL, into Node applications. One approach uses mysql (node-mysql), a popular JavaScript MySQL client. The other approach uses db-mysql, which is part of the new node-db initiative to create a common framework for database engines from Node applications. The db-mysql module is written in C++.

Neither of the modules mentioned currently supports transactions, but mysql-series has added this type of functionality to node-mysql. I’ll provide a quick demonstration on this, and also offer a brief introduction to Sequelize, an ORM (object-relational mapping) library that works ...

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