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 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-request-parser

v1.0.3

Published

Parser for request object

Downloads

14

Readme

node-request-parser

Description

The module node-request-parser represents a parser for requests in node and express. This parser is able to extract properties from the request object, sanitize them with a given sanitizer function and return proper errors if the properties you expect are missing. Additionally you can pass an authorization function that the parser uses to extract i.e. a header property and map it to a user object. And all this is done with one single call!

Install

Use NPM to install node-request-parser and add dependency to package.json:

$ npm install --save node-request-parser

Usage

Initialize

var RequestParser = require('node-request-parser');
//detailed description of options object down below
var options = {disableRegex: true};
var parser = new RequestParser(options);

Keys

Keys are used to specify what properties of the request you want to parse and how you want them. Let me explain it with an example:

'B*id?'

This key means, you want a property from request.body that is named id, you want it to be sanitized using your specified sanitizer function and you say it is optional, so no error is returned if the property is missing.

So keys can be as follows:

  • Starting with one of the following characters:
    • B for body
    • P for params
    • H for headers
    • Q for query
    • A for authorization
  • an optional *, if given then the property is sanitized
  • the name of the property you want to parse
  • an optional ? to mark the property as optional

If the key A or A? is used, there is no need to pass * or a property name, only the given authFunction is executed.

Parsing async

Use i.e. in express route endpoints:

router.get('/foo', function(req, res, next) {
    parser.parse(req, ['Bid'], function(err, data) {
        if(err){
            /*
            if an error occures and error === 'parser_error',
            then data is an array with more detailed errors like i.e.:
            data = ['body_missing_id']
            */
        } else{
            /*
            if everything is okay, the data object should look like:
            data = {
                body: {
                    id: 'theParsedId'
                }
            }
            */
        }
    });
});

Parsing sync

Use i.e. in express route endpoints:

router.get('/foo', function(req, res, next) {
    var data = parser.parseSync(req, ['Bid']);
    if(data.error){
        /*
        if an error occures and data.error === 'parser_error',
        then data.errors is an array with more detailed errors like i.e.:
        data.errors = ['body_missing_id']
        */
    } else{
        /*
        if everything is okay, the data object should look like:
        data = {
            body: {
                id: 'theParsedId'
            }
        }
        */
    }
});

Options

The options object with its default values:

var options = {
    /**
    * an array of strings according to the specification of node-request-parser,
    * that are always parsed from a request
    */
    alwaysParse: [],
    
    /**
    * an array of strings, which js types to exclude from sanitizing generally
    */
    excludeSanitizeTypes: ['boolean', 'number'],
    
    /**
    * a sanitize function
    */
    sanitizeFunction: undefined,
    
    /**
     * has to be a function that requires request headers and callback(err, user) and then returns user
     * object(or every object you like extracted from the headers) like getUserFromRequest(headers, callback)
     */
    authFunction: undefined,
    
    /**
     * disables regex check on input keys
     */
    disableRegex: false
}

alwaysParse

Put all keys in that you want to have parsed every time you use the parser.

var alwaysParse = ['B*id', 'H*language'];

excludeSanitizeTypes

Put in all js types you want to generally exclude from sanitizing. The types boolean and number are excluded by default if you don't specify different. This is because most sanitizers are only able to sanitize strings and passed numbers or booleans would be converted to strings.

var excludeSanitizeTypes = ['boolean', 'number'];

sanitizeFunction

A possibility to automatically sanitize parsed properties.

var sanitizeFunction = function(propertyToSanitize) {
    return sanitizer.sanitize(propertyToSanitize);
}

authFunction

A possibility to lookup authorization data with a parser call. Does not work with parseSync().

var authFunction = function(headers, callback) {
    //example database call
    database.findUserWithCookie(headers.customCookie, function(err, user) {
        if(err) callback(err, null);
        callback(null, user);
    });
}

disableRegex

Set to true if you trust yourself and like to save computation time through disabling regex check of each key.

License

This code available under the MIT License. See License.md for details.