Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-relax

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-relax

Wraps nano's API as hapi server methods and seamlessly authenticates with cookie

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

Build StatusCode ClimateTest CoverageDependency Status

hapi-relax

hapi plugin that registers server methods using nano and extends it by taking care of cookie authentication

Introduction

hapi-relax enhances your hapi server by an interface for a specified CouchDB database.
If you're using cookie authentication on top of basic-auth you can also pass a username and password. Just relax and stop worrying about authentication because the plugin will check if you are authorized. If not it will use nano's auth method to get a cookie and use that in a further request. But you won't notice because that all happens internally.
It will also update its cookie when CouchDB sends a new cookie in the headers.

Usage

var Hapi = require('hapi');
var server = new Hapi.Server();
var options = { nano: { db: 'testDb' } };

server.connection({ port: 80 });

server.register({ register: require('hapi-relax'), options }, function(err) {
});

server.start();

The plugin takes the following options:

  • nano - optional config object which nano will be initialized with.
    db is required. Defaults to { url: 'http://localhost:5984' }
  • user - optional database username
  • password - optional password for your database user; required if user is passed
  • prefix - optional namespace in which the server methods will be registered;
    required if the plugin is registered multiple times e.g. for multiple databases or hosts. Defaults to nano

API

nano's API remains unchanged

  • info
  • replicate
  • compact
  • changes
  • follow
  • session
  • insert
  • get
  • head
  • copy
  • destroy
  • bulk
  • list
  • fetch
  • fetchRevs
  • show
  • atomic
  • updateWithHandler
  • search
  • spatial
  • view
  • viewWithList
  • multipart: {insert, get}
  • attachment: {insert, get, destroy}

Example

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 8080 });

var plugins = [{
  register: require('hapi-relax'),
  options: {
    nano: {
      url: 'http://localhost:5984',
      db: 'db1'
    },
    user: 'root',
    password: 'secret'
  }
},
{
  register: require('hapi-relax'),
  options: {
    nano: {
      url: 'http://localhost:5984',
      db: 'db2'
    },
    user: 'alice',
    password: 'rabbit',
    prefix: 'db2'
  }
}];

server.register(plugins, function (err) {
  if (err) {
    throw err;
  }
});

server.route({
    method: 'GET',
    path: '/db1/{key}',
    handler: function (request, reply) {
      var key = encodeURIComponent(request.params.key);
      server.methods.nano.get(key, function (err, body, headers) {
        if (err && err.reason === 'missing') {
          reply('Document does not exist').code(404);
        }
        else {
          reply(body);
        }
      });
    }
});

server.route({
    method: 'GET',
    path: '/db2/{key}',
    handler: function (request, reply) {
      var key = encodeURIComponent(request.params.key);
      server.methods.db2.get(key, function (err, body, headers) {
        if (err && err.reason === 'missing') {
          reply('Document does not exist').code(404);
        }
        else {
          reply(body);
        }
      });
    }
});

server.start(function () {
  console.log('Server running at:', server.info.uri);
});

Keywords

FAQs

Package last updated on 01 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc