Friday, June 21, 2013

node.js Notes

Good advice re: learning node.js is here.


$ npm shrinkwrap   is like   $ pip freeze   Explanation here.


Connecting to local mondoDB server or a cloud mongoDB is simply a matter of creating the connection URL properly. Do A or B below.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var url;

module.exports.mongoose = mongoose;
module.exports.Schema = Schema;

// (A) Connect to cloud database (like mongolab)
var username = "user";
var password = "password";
var address = '@dbh42.mongolab.com:27427/nockmarket';
url = 'mongodb://' + username + ':' + password + address;
connect(url);

// (B) Connect to DB called chapter1 on local mongoDB server
url = 'mongodb://localhost/chapter1';
connect(url);

// Connect to mongo
function connect(u) {
  mongoose.connect(u);
}

function disconnect() {mongoose.disconnect()}


No comments:

Post a Comment