npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

assist

v0.3.2

Published

A set of helpers for small, modular middleware libraries

Downloads

39

Readme

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

...