2.0.0 • Published 9 years ago
headary v2.0.0
headary - summarize HTTP headers
headary is a trivial Node package that provides a normalized summary of basic HTTP status codes and headers. You might use headary to write control flow for redirects and 304s in a uniform way. This can make sense if you handle responses at multiple locations in your code.
Example
const headary = require('headary')
// Get HTTP response `res` from somewhere.
const h = headary(res)
if (h.ok) {
// Move on.
} else {
if (h.message) {
// Quaint or unhandled HTTP status.
const er = new Error(h.message)
this.emit('error', er)
} else if (h.url) {
// Issue request with new URL.
if (h.permanent) {
// Update some cache or whatever.
}
} else if (h.permanent) {
// `410: Gone`, update cache.
} else {
// `304: Not Modified`, done.
}
}Types
Headers
messageStringOptional information.okBooleanThis flag istrueif no further actions are required.permanentBooleanIf the resource has been moved permanently, this istrue.urlStringIf the resource has been moved, this is its new location.
Exports
headary exports a single function that returns a new Headers object.
headary(res)
reshttp.IncomingMessageA HTTP response.
Creates Headers from a HTTP response.
The considered HTTP status codes:
200OK300Multiple Choices301Moved Permanently302Found303See Other304Not Modified305Use Proxy307Temporary Redirect410Gone
Install
With npm do:
$ npm install headary