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

express-ez-input-handler

v1.0.0

Published

An elegant input handler for express requests/responses to ease the management of input/ouput data

Downloads

6

Readme

Express EZ input handler

An elegant input handler for express requests/responses to ease the management of input/ouput data

Build Status codecov

Installation

npm install express-ez-input-handler --save

Basic usage

This is a basic example on how to define and use the validator.

const express = require('express'); // Works with version 3+ of express
const inputHandler = require('express-ez-input-handler');

const app = express();
app.use(express.json()); // Note that this method is available on express v4.16+

const getSchema = {
  title: true,
  tags: true,
  author: true
};

app.get('/news', inputHandler({ schema: getSchema }), (req, res) => {
  console.log(JSON.stringify(req.schema));
  res.send(200);
});

const postSchema = {
  title: true,
  tags: true,
  author: true
};

app.post('/news', inputHandler({ schema: getSchema }), (req, res) => {
  console.log(JSON.stringify(req.schema));
  res.send(200);
});

If we make an GET call to /news?title=%express%&tags=node&tags=express&tags=node.js* OR a POST call to /news with a body:

{
  title: '%express%',
  tags: ['node', 'express', 'node.js'],
}

The log would return, in both cases:

{
  title: '%express%',
  tags: ['node', 'express', 'node.js'],
}

By default, this library does not follow strict REST conventions, where GETs cannot have a data in the request body, and POSTs cannot have data in the URL. This library is greedy by default, so any data on the body of a GET request will be fetched, validated and transformed. If you want to disable this behaviour, you should create the middleware like this:

const getSchema = {
  title: true,
  tags: true,
  author: true
};

const options = {
  strict: true
};

app.get('/news', inputHandler({ schema: getSchema, options }), (req, res) => {
  console.log(JSON.stringify(req.schema));
  res.send(200);
});

Better usage

This may prove usefull to avoid getting request data from the 3 sources of the request: query, params and body. But, the real potential of this library is that you can enable different functionalities to avoid lots of tedious and repetitive work. For instance:

Enabling validation

const express = require('express'); // Works with version 3+ of express
const inputHandler = require('express-ez-input-handler');

const app = express();
app.use(express.json());

const schema = {
  title: {
    required: true,
    string: {
      length: { min: 5, max: 100 }
    }
  },
  tags: {
    required: true,
    array: {
      length: { min: 1, max: 5 },
      string: {
        length: { min: 1, max: 5 },
      }
    }
  },
  author: {
    object: {
      name: {
        required: true,
        string: {
          length: { min: 5, max: 100 }
        }
      },
      age: {
        number: {
          isInteger: true,
          range: { min: 18 }
        }
      }
    }
  }
};

app.post('/news', inputHandler({ schema }), (req, res) => {
  console.log(JSON.stringify(req.schema));
  res.send(200);
});

This schema has validation on it. Using ez-validate.js this library now validates the input AND handles errors for you. So, when the input arrives to the controller, its nice, clean, sanitized and validated. You don't have to care about where on the request is the data, or if its valid.

Errors

By default, this library returns http standard errors if it should. You can, however, in future versions it will support custom responses