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

mosh

v1.1.5

Published

A simple value watcher for Nodejs (Express) Apps

Downloads

321

Readme

mosh.js

A simple null / empty checker

mosh.js is a simple tool for null or undefined or falsey value checks in node.js apps. It provides a neat abstraction for if-else blocks and relieves the developer of the headaches that come with logic-fuzz.

How to use

Intall using npm sudo npm install mosh

Require mosh in your code and init as a middleware before your route definitions

var mosh = require('mosh'),
    express  = require('express');
app.set('view engine', 'ejs');
app.set('views', './public/views');
var app = new express();
//Init mosh
app.use(mosh.initMosh);

//Example route using mosh checks
app.get('/', function (req, res, next) {
    res.mosh.emptyCheck(req.query.access_token, 'Access token needed to view this page');
    res.mosh.callE('render','index', {user:'mosh',page:'Index'});
});

//Init mosh's error handler
app.use(mosh.initMoshErrorHandler);

One caveat with using mosh. Unlike php where we can easily use die() / exit() to stop code execution. Node.js has no 'easily' implementable equivalent functions. calling res.render or any other response.end / response header change invoking functions after mosh causes a 'header already sent' error. mosh handles this by using the mosh.callE wrapper for such functions described above. The only thing you need to do is include the function name as the first argument to mosh's callE function and subsequent ones as you would normally (and in the same order) pass to the function. So, res.render('ejsfilename',ejstemplatepayload) becomes res.mosh.callE('render','ejsfilename',ejstemplatepayload) AND res.json({random:1234}) becomes res.mosh.callE('json',{random:1234})

Example Use-case 1

Typical flow

var secure = req.param('secure');
if (!secure) {
    res.forbidden('This request has to be secure!');
}else{
  //other stuff here
}

mosh flow

var secure = req.param('secure');
res.mosh.emptyCheck(secure, 'This request has to be secure', 'forbidden');
//If secure is empty, code won't go beyond the mosh.emptyCheck call
// other stuff here

Example Use-case 2

Typical flow

var client_id     = req.query.client_id;
var client_secret = req.query.client_secret;
//imagine we have more required (query) params 
if(!client_id || !client_secret){
   res.fail('client_id and client_Secret are required');
}
//other app stuff

mosh flow

res.mosh.multiArrayCheck(req.query, ['client_id','client_secret']); 
//Code won't go beyond the mosh.multiArrayCheck call if any of the passed values fail.
var client_id     = req.query.client_id;
var client_secret = req.query.client_secret;

Example Use-case 3

Typical flow

var user_id = req.params.user_id;
if(!user_id)
{
	res.fail('User  id is required');	
}
else
{
	
	UserModel.findOne(user_id, function(user, err){
		if(!user){
		   res.fail('User not found');
		}
		else
		{
			//other stuff to happen if user is found
	    }
	});

}

mosh flow

res.mosh.emptyCheck(req.params.user_id, 'User id is required', 'fail');
var user_id = req.params.user_id;
UserModel.findOne(user_id, function(user, err){
	res.mosh.emptyCheck(user, 'User not found', 'fail');
	//other stuff to happen here
});

Example Use-case 4

Typical flow

if(req.body.IGUser && req.body.IGuser.handle){
	var IGUserHandle = req.body.IGUser.handle;
}
else{
	res.fail('Required handle property not found in USER');
}

mosh flow

res.mosh.multiDepthCheck(req.body, ['IGuser', 'handle'],'Required handle property not found in USER');
var IGUserHandle = req.body.IGUser.handle

Use Mosh outside of express

var Mosh = require('mosh');
var mosh = new Mosh(null); //Don't pass a res object

mosh.emptyCheck(false, 'You shall not pass'); //Throws an error with the message you shall not pass.
mosh.multiArrayCheck({'name':'Lawal',token:'0399940'}, ['client_id','client_secret']);  //Throws an error