express-api-helper
Simple API helper module for Express apps.
API
ok(req, res, data)
Respond with 200 OK
and JSON-encoded data.
req
express Requestres
express Responsedata
Object
badRequest(req, res, errors)
Respond with 400 Bad Request
and JSON-encoded error object, {message:String,errors:Array}
.
req
express Requestres
express Responsedata
Array (of String) or String
unauthorized(req, res)
Respond with 401 Unauthorized
and JSON-encoded error object, {message:String}
.
req
express Requestres
express Response
forbidden(req, res)
Respond with 403 Forbidden
and JSON-encoded error object, {message:String}
.
req
express Requestres
express Response
notFound(req, res)
Respond with 404 Not Found
and JSON-encoded error object, {message:String}
.
req
express Requestres
express Response
unsupportedAction(req, res)
Respond with 405 Method Not Allowed
and JSON-encoded error object, {message:String}
.
req
express Requestres
express Response
invalid(req, res, errors)
Respond with 422 Unprocessable Entity
and JSON-encoded error object, {message:String,errors:Array}
.
req
express Requestres
express Responseerrors
Array (of String) or String
serverError(req, res, error)
Respond with 500 Internal Server Error
and JSON-encoded error object, {message:String,error:Object}
.
req
express Requestres
express Responseerror
Object
requireParams(req, res, params, callback)
Require that listed parameters are present. Checks for presence of each parameter in req.body
object if using express.bodyParser
middleware; otherwise checks for presence of each parameter in req.params
or req.query
. If any parameters are missing, invokes badRequest
with an array of error messages with the form "Missing required parameter: %s"
.
req
express Requestres
express Responseparams
Array (of String) or Stringcallback(err)
Function
requireHeaders(req, res, headers, callback)
Require that listed headers are present. Checks for presence of each header in req.headers
. If any parameters are missing, invokes badRequest
with an array of error messages with the form "Missing required header parameter: %s"
.
req
express Requestres
express Responseheaders
Array (of String) of Stringcallback(err)
Function
Example
Sample usage:
var http = express = bodyParser = api = app = Post = ; app;app; appall'/api/*' { if !requser return api; ;}; app; app; app; http;
Running Tests
To run the tests, clone the repository and install the dev dependencies:
git clone git://github.com/paambaati/express-api-helper.gitcd express-api-helper && npm installmake test