sendhal
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 = ;var path = ;var logger = ;//var cookieParser = require('cookie-parser');var bodyParser = ;var sendhal = ;var models = ;// create database connectionvar db = models;models; var routes = ;var app = ;app;app;app;app; // reuse the db connection on any requestapp;app; /// catch 404 and forwarding to error handlerapp; app; moduleexports = app;
./routes/index.js
// ... var sendhal = ;// ... router; router
API
send statusCode res.statusCode or 200
the URI of _self is set by req.originalUrl
- doc: resource content
- req: express request
- res: express response
send statusCode 201
set header 'location:' to req.path + id
- id: the new resource id
- req: express request
- res: express response
send not found response with code 404
- req: express request
- res: express response
implementation of express error handler interface
The err parameter is used to switch the output
- any other: statusCode 500
You can set the status code with err.status or res.statusCode
Resource
the hal resource object