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

howhap-middleware

v4.1.3

Published

Easily integrate howhap errors into your express app.

Downloads

34

Readme

Houston, we have a problem - express middleware

A small library for displaying errors from express

npm install --save howhap

app.js

let express = require('express');
let howhap = require('howhap-middleware');

// An *optional* preset list of errors.
let options = {
	errors: {
		DEFAULT: {
			message: 'An unknown error occurred.',
			status: 500,
			level: 'error'	// this property specified what
							// level of logging is associated
							// with this error.
		},
		AUTH: {
			INVALID_EMAIL: {
				message: '{{ email }} is not a valid email.',
				status: 400
			},
			MISSING_PASSWORD: {
				message: 'Please enter a password.',
				status: 400
			}
		}
	},
	defaultFormat: 'json',	// If no response format is supplied,
							// responses will be in this format.
	logging: {
		// options for winston loggin go here
	}
};

let app = express();

// Pass in the option argument to customize the behavior 
// of the middleware. Alternatively you can pass in nothing
// and use the default behavior.
app.use(howhap(options));

example route

let express = require('express');
let router = express.Router();
let validator = require('validator');

router.post('/login', function(req, res, next) {
	let minPasswordLength = 8;
	// You can call a preset error by using dot notation to
	// traverse the predefined object and pass in parameters
	// as the second argument. Errors are keyed by any string
	// for later use. In this case 'email'.
	if(!validator.isEmail(req.body.email)) {
		res.error.add('AUTH.INVALID_EMAIL', {email: req.body.email}, 'email');
	}
	// Parameters are not required. Nor is the error key,
	// which will be 'default' if none is specified.
	if(!req.body.password) {
		res.error.add('AUTH.MISSING_PASSWORD');
	}
	// You can also add custom error messages on the fly.
	// Parameters can be specified in one object or passed
	// in as a second argument like in the email example.
	else if(req.body.password.length < minPasswordLength) {
		res.error.add({
			message: 'Your password must be at least {{ num }} chars long.',
			status: 400,
			params: {
				num: minPasswordLength
			}
		}, 'password');
	}

	// Other code goes here...

	// Send the errors and redirect back to the login page.
	// If no argument is passed to the first argument of the
	// send method then you will redirect back to the referer.
	// The send method will return false if there are no errors
	// to send. The send method takes a optional second argument
	// specifying the response format you would like ('html' or
	// 'json'). The format will be resolved in the following
	// order:
	//
	// 1. The second argument of res.error.send
	// 2. If (1) not specified then the responseFormat query
	//    parameter
	// 3. If (2) not specified then the defaultFormat middleware
	//    option
	// 4. If (3) not specified then 'json'
	//
	// If the resolved format is 'html' the user will be
	// redirected to the specified redirect. Otherwise json will
	// be returned.
	if(!res.error.send('/login', 'html')) {
		// There were no errors, redirect.
		res.redirect('/dashboard');
	}

	// Here's an example of sending json
	//
	// if(!res.error.send(null, 'json')) {
	// 	res.json(user);
	// }
});

example view

<!DOCTYPE html>
<html>
	<head>
		<title>Log in</title>
		<link rel='stylesheet' href='/styles/main.scss.css' />
	</head>
	<body>
		<h1>Log in</h1>
		<form action="/auth/login" method="post">
			<div>
				<!--
				   - An `error` object is passed to all views, with a
				   - display method. The first argument is the key
				   - of the error that you'd like to display. The
				   - second (optional) argument is a default value
				   - that you'd like to display if there is no error
				   - for the specified key.
				   -->
				<%= error.display('email') %>
				<!--
				   - A `prev` object is also passed to all views,
				   - which stores the previous data that was posted
				   - (body, query, and params). This data is only set
				   - if there was an error. It takes two arguments
				   - (the type of data and the key) as well as an
				   - optional third argument if there was no data
				   - found for that particular key.
				   -->
				<input type="email" placeholder="email" name="email" value="<%= prev.display('body', 'email') %>">
			</div>
			<div>
				<!-- Example of using the optional default (second argument) -->
				<%= error.display('email', 'Bad password') %>
				<input type="password" placeholder="password" name="password">
			</div>
			<button>Log in</button>
		</form>
	</body>
</html>