Assist
Install: npm install assist
Assist provides a few conveniences you might be used to from frameworks like express
, but packaged as middleware.
Helpers include:
req.body
res.send()
res.json()
res.render()
res.redirect()
res.text()
req.body
var http = ;var sw = ;var assist = ; var bodyParser = assistbody url: true // Parse urlencoded payloads json: true // And JSON multipart: false // Don't accept chunked multipart payloads maxLength: 8192 // The maximum length for a payload (doesn't work with multipart right now);var middlewareStack = bodyParser; { var name = reqbody; res;} var app = sw;http;
res.send()
... var middlewareStack = assist; // Call it as a factory, it returns a new middeware. { var name = reqbody; res; // The status code and Content-Type can be changed using options: res;} ...
res.json()
... /*Basically just `res.send()` with a Content-Type of application/jsonand automatical stringification.*/var middlewareStack = assist; { // Just drop in any object you want to send to the client var r = response: 'angry' errcode: 2319 messages: 'I can\'t feel my face!' 'Try using less glue.' ; res; // The client needs this data, for some reason... res; // Set status codes with the second argument} ...
res.render()
... // This is just a bridge that takes a render function and staples it to `res` for easy access// Just like json, render requires `res.send()` var helloTemplate = hogan;// Args: request object, name of the template to render, data used to render the template { ifname == 'hello' return helloTemplate; return '';}var middlewareStack = assist assist; { res;} ...
res.redirect()
... // Just a quick way to send a Location headervar middlewareStack = assist; { // If called without the status code, it defaults to 302 (temporary redirect) res;} ...
res.text()
.../*Send content as text/plain. Supports same options as send except for type,which is always text/plain.Options can be replaced with a number to indicate the status code to send.Mostly useful for making REST APIs.*/var middlewareStack = assisttext; { var name = reqbody; restext'Hello ' + name; restext'What? Where am I?!' code: 404 // Status code charset: 'ISO-8859-1' // Defaults to utf-8, can be removed by passing `false` ; // Or for short: restext'Hmm...' 404;} ...