assist

0.3.2 • Public • Published

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 = require('http');
var sw = require('simpleware');
var assist = require('assist');
 
var bodyParser = assist.body({
    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];
 
function serve(req, res) {
    var name = req.body;
    res.end('Hello ' + name);
}
 
var app = sw.mw(middlewareStack, serve);
http.createServer(app).listen(80);

res.send()

...
 
var middlewareStack = [assist.send()]; // Call it as a factory, it returns a new middeware.
 
function serve(req, res) {
    var name = req.body;
    res.send('Hello ' + name);
 
    // The status code and Content-Type can be changed using options:
    res.send('What? Where am I?!', {
        code: 404, // Status code
        type: 'text/plain', // Defaults to text/html
        charset: 'ISO-8859-1', // Defaults to utf-8, can be removed by passing `false`
    });
}
 
...

res.json()

...
 
/*
Basically just `res.send()` with a Content-Type of application/json
and automatical stringification.
*/
var middlewareStack = [assist.json()];
 
function serve(req, res) {
    // 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.json(r); // The client needs this data, for some reason...
 
    res.json(r, 201); // 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.compile('Hello {{name}}, you are {{age}} years old');
// Args: request object, name of the template to render, data used to render the template
function renderTemplate(req, name, data) {
    if(name == 'hello') {
        return helloTemplate.render(data);
    }
    return '';
}
var middlewareStack = [assist.send(), assist.render(renderTemplate)];
 
function serve(req, res) {
    res.render('hello', {name: 'Harold', age: 47});
}
 
...

res.redirect()

...
 
// Just a quick way to send a Location header
var middlewareStack = [assist.redirect()];
 
function serve(req, res) {
    // If called without the status code, it defaults to 302 (temporary redirect)
    res.redirect('/to/a/deep/dark/scary/directory', 301);
}
 
...

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 = [assist.text()];
 
function serve(req, res) {
    var name = req.body;
    res.text('Hello ' + name);
 
    res.text('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:
    res.text('Hmm...', 404);
}
 
...

Readme

Keywords

none

Package Sidebar

Install

npm i assist

Weekly Downloads

1

Version

0.3.2

License

none

Last publish

Collaborators

  • download