Book Image

Node.js Blueprints

By : Krasimir Stefanov Tsonev
Book Image

Node.js Blueprints

By: Krasimir Stefanov Tsonev

Overview of this book

Table of Contents (19 chapters)
Node.js Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with the database


In Chapter 3, Writing a Blog Application with Node.js and AngularJS, we used MongoDB and MySQL. We learned how to read, write, edit, and delete records from these databases. Let's use MongoDB in this chapter, too. We will store our data in a collection named books. To use the database driver, we need to create a package.json file and put the following content in it:

{
  "name": "projectname",
  "description": "description",
  "version": "0.0.1",
  "dependencies": {
    "mongodb": "1.3.20"
    "request": "2.34.0"

  }
}

After running npm install, we will be able to connect to the MongoDB server by using the driver installed in the node_modules directory. The code that we need to interact with the database is the same as the one used in Chapter 3, Writing a Blog Application with Node.js and AngularJS, which is as follows:

var crypto = require("crypto"),
    client = require('mongodb').MongoClient,
  mongodb_host = "127.0.0.1",
  mongodb_port = "27017",
  collection;...