express-rendertype
Express middleware to (automatically) render content and errors to content-type
This is basically a mashup of
- express-errs (Rails-inspired error handler for Express applications), and
- errorhandler (full stack traces and util.inspect of objects),
- a nod to express-error-responses,
- with a helper for restify/errors Error Classes thrown in.
Installation
npm install --save express-rendertype
Usage
render = require "express-rendertype" app = express
Rendering content
render.auto(fallback = false, preference = ["yaml", "json", "html", "text"])
create a .rendr method on the res object, falling back to json as default
appuse renderauto "json"
res.rendr(path = "", obj)
render to json or whichever content-type the client accepts
appget "/someroute" resrendr "path/to/template"obj
Rendering errors
Status code
pass an error from just a status code
apppost "/noauthroute" next status: 401
Error library
create an error with stacktrace in context from a status code
appput "/payme" next rendererrorfromCode 402
throw an error with message from the error library
appget "/gatekeeper" throw "I am the keymaster"
See also: restify/errors
Objects
pass an object as the error
appdelete "/session" next foo: "bar"baz: "booze"
(Fancy) Error-handling middleware
add the error-handling middleware after all routes
appget ...apppost ...appput ......appuse renderFancyErrorsauto "text" if @appget "env" is "development"appuse renderErrorsauto
render.FancyErrors.auto(fallback, preference, log)
render.Errors.auto(fallback, preference, log)
Errors and FancyErrors are the same, except FancyErrors will log the stack to all content-types. FancyErrors even has a nice code excerpt when rendered to HTML.
appuse renderErrorsauto "html""html""json"consolelog
MIT Licensed. (C) 2016 doublerebel