node package manager


Add hypermedia awareness to your REST services.


Add hypermedia awareness to your REST services.

leisure offers:

  • Content Negotiation for preferred media type.
  • Automatically sets response Content-Type.
  • Work in terms of hypermedia.
var express = require('express');
var leisure = require('leisure');
var accept = leisure.accept;
var app = express.createServer();
var dashboard = require('./media/dashboard');
app.get('/', accept(dashboard.mediaTypes), function(req, res) {
  var media = dashboard.create(req.accepted);
exports.mediaTypes = [
  { contentType: 'application/', formats: ['json', 'xml'] },
  { contentType: 'application/json' },
  { contentType: 'text/xml' }
exports.create = function(mediaType) {
  var mediaFactory = {
    'application/': createDashboardMedia(mediaType.format),
    'application/json': createDashboardMedia('json'),
    'text/xml': createDashboardMedia('xml')
  var media = mediaFactory[mediaType.contentType]();
  return media;
function createDashboardMedia(format) {
    return function mediaFormat() {
      var formats = {
        'json': '{ "account": { "href":  "/account" }, "products": { "href": "/products" } }',
        'xml': '<dashboard><account href="/account" /><products href="/products" /></dashboard>'
      return formats[format];

Launch the app.

$ node app.js

Set the Accept header on a curl request to http://localhost:3000.

$ curl -H "Accept: application/" -X GET "http://localhost:3000"
{ "account": { "href":  "/account" }, "products": { "href": "/products" } }


$ npm install leisure

leisure.accept is connect-compatible middleware that compares a list of accepted media types to those supported by the client via the HTTP Accept header and selects the most preferred media type available. The preferred media type is appended to the request as req.accepted. The mediaTypes parameter is required.

mediaTypes is an array of acceptable media types for the request. Each media type in the array needs a contentType property with the appropriate media type and an optional formats property listing acceptable formats.


  { contentType: 'application/', formats: ['json', 'xml'] }, 
  { contentType: 'application/json' }

The mediaTypes array is prioritized by index. In the example above, the preference order is:

  1. application/
  2. application/
  3. application/json

options is a collection of options for the current middleware instance.


leisure.accept([{ contentType: 'text/xml' }], { strictMode: true });

strictMode sends a 406 Not Acceptable response when no media type is matched. The default value is taken from leisure.options.strictMode.

Global options in leisure.

console.log(leisure.options.strictMode); // => false 

setOption sets a global option in leisure.

When the strictMode option is true, leisure sends a 406 Not Acceptable response when there is no matching Accept value in the request. When strictMode is false, leisure will respond with the most preferable media type. The default value is false. This can be overriden for each middleware instance (see leisure.accept).

leisure.setOptions('strictMode', true);

Tests are written in mocha. To run, use:

$ npm test