Skip to content

An express middleware and extension to enable HAL responses with a simple API

Notifications You must be signed in to change notification settings

dominicbarnes/express-hal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-hal

An implementation of HAL for Express.js.

  • Exposes a res.hal method for automatically assembling a JSON-HAL response body.
  • Adds a req/res.resource object for building your resource prior to res.hal

Installation

npm install express-hal

Usage

// app.js
var hal = require("express-hal");

// before the router middleware
app.use(hal.middleware);

// routes
app.get("/orders", function (req, res, next) {
    res.hal({
        data: {
            currentlyProcessing: 14,
            shippedToday: 20,
        },
        links: {
            self: "/orders",
            next: "/orders?page=2",
            find: { href: "/orders{?id}", templated: true }
        },
        embeds: {
            "orders": [
                {
                    data: {
                        total:    30.00,
                        currency: "USD",
                        status:   "shipped"
                    },
                    links: {
                        self:     "/orders/123",
                        basket:   "/baskets/98712",
                        customer: "/customers/7809"
                    }
                },
                {
                    data: {
                        total:    20.00,
                        currency: "USD",
                        status:   "processing"
                    },
                    links: {
                        self:     "/orders/124",
                        basket:   "/baskets/97213",
                        customer: "/customers/12369"
                    }
                }
            ]
        }
    });
});

About

An express middleware and extension to enable HAL responses with a simple API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published