0.3.0-beta • Published 3 years ago

@bouncingpixel/universal-response v0.3.0-beta

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

universal-response

Exposes a set of OK universal responses for working with XHR and non-XHR clients.

Working With

Requirements

  • NodeJS 0.10+
  • Express 4+

Using universal-response

Add the main middleware to expose the functions on the res object.

app.use(require('@bouncingpixel/universal-response'));

The following response utilities are added:

  // defined if req.xhr or if the req only accepts json
  req.wantsJSON: boolean

  // ok will either render the view if the request is not XHR nor wants JSON
  // or will send the data as JSON for XHR/wants JSON requests
  res.ok(view: string, data: any)

  // ok will either redirect to a page if the request is not XHR nor wants JSON
  // or will send the data as JSON for XHR/wants JSON requests
  res.okRedirect(page: string, data: any)