GithubHelp home page GithubHelp logo

jensklose / sendhal Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 204 KB

Express middleware and error handler to send hal responses with json/xml/html negotiation.

JavaScript 1.10% CoffeeScript 98.90%

sendhal's Introduction

sendhal

Build Status

Express middleware and error handler to send hal responses with json/xml/html negotiation.

Install

npm install sendhal --save

Usage

This example shows the basic usage in an express 4 context.

    var express = require('express');
    var config = require('config');
    var sendhal = require('sendhal');
    var app = express();
    
    app.route('/api').get(function(req, res, next) {
        var rootRelations, hal, rel, _ref;
        hal = sendhal.Resource({welcome: 'api entry'}, req.path);
        _ref = config.relations;
        for (rel in _ref) {
            rootRelations = _ref[rel];
            hal.link(rel, rootRelations);
        }
        return sendhal.ok(hal, req, res);
    });
    
    app.use('/api', function(req, res, next) {
        var err, method;
        method = req.method.toLowerCase();
        if (method === 'get') {
            err = new Error('Not Found');
            res.status(404);
            return sendhal.fail(err, req, res, next);
        }
        res.status(405);
        return sendhal.fail(new Error('Method Not Allowed'), req, res, next);
      });
    app.use('/api', sendhal.fail);

API

ok

ok(doc, req, res)

send statusCode res.statusCode or 200
the URI of _self is set by req.originalUrl

  • doc: resource content
  • req: express request
  • res: express response

created

created(id, req, res)

send statusCode 201
set header 'location:' to req.path + id

  • id: the new resource id
  • req: express request
  • res: express response

notFound

notFound(req, res)

send not found response with code 404

  • req: express request
  • res: express response

fail

fail(err, req, res, next)

implementation of express error handler interface
The err parameter is used to switch the output

  • [object Array]: statusCode 400; used for validation errors
  • [object Error]: statusCode 500; used for server errors
  • any other: statusCode 500

You can set the status code with err.status or res.statusCode

Resource

Resource(object, uri)

the https://github.com/naholyr/js-hal resource object

sendhal's People

Contributors

jensklose avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.