Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including hapi-mongodb2 with all npm packages installed. Try it out:

require("hapi/package.json"); // hapi is a peer dependency. require("mongodb/package.json"); // mongodb is a peer dependency. var hapiMongodb2 = require("hapi-mongodb2")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

hapi-mongodb2 v2.0.1

Hapi's MongoDB 2.0 native driver plugin

hapi-mongodb2

Hapi (^8.0) plugin for the MongoDB native driver 2.0.

Install

npm install --save mongodb@2 hapi-mongodb2

Register plugin

var Hapi = require('hapi');
var server = new Hapi.Server();

server.register({
  register: require('hapi-mongodb2')
  opts: { url: 'mongodb://user:password@domain.tld:port/database' }
}, function (err) {
  if (err) console.error(err);
});

Use plugin

The object returned by MongoClient.connect is exposed on server.plugins['hapi-mongodb2'].client and binded to the context on routes and extensions as this.mongo.

If by any chance you need to use the mongodb library itself you can get it from server.plugins['hapi-mongodb2'].library.

server.route({
  method: 'GET',
  path: '/users',
  handler: function (request, reply) {
    var mongo = request.server.plugins['hapi-mongodb2'].client;
    mongo.collection('users').find({}).toArray(function (err, users) {
      reply(users);
    });
  }
}, {
  method: 'GET',
  path: '/databases',
  handler: function (request, reply) {
    var mongo = this.mongo;
    mongo.admin().listDatabases(function (err, dbs) {
      reply(dbs);
    });
  }
});

License

Licensed under the terms of the ISC. A copy of the license can be found in the file LICENSE.

© 2015, Jose-Luis Rivas <me@ghostbar.co>

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free