New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

assist

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assist

A set of helpers for small, modular middleware libraries

  • 0.3.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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);
}

...

FAQs

Package last updated on 28 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc