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

request-param

v1.0.1

Published

connect/express middleware to enable back the `req.param(name,default)` API depricated in express 4

Downloads

9,281

Readme

request-param Build Status

request-param

It started as a connect/express middleware to enable back the req.param(name,default) API depricated in express 4.

My speculation is that they did not want to get in the discussion of what should take precedence between routed args, query string and post body, and roll the policy on the application maker.

But it actually does a little more than that.

If you just want backward compatibility with express 3 or earlier - you can use this middleware.

But read on - you can do a little more :)

usage

var app = require('express')()

app.use( require('request-param')() )

app.post( '/some/:value', function(req,res,next) {
    req.param('value', 'dff'); // returns :value from path
                               // or ?value=<value> from query
                               // or body.value from body
                               // or 'dff' as default value
})

Determine default order on our own

If you would like to determine the order in which collections are searched:

app.use( require('request-param')({ order: ["body","params","query"] } ) )

You can name whatever collection is found on the request object, including objects initiated by other middlewares:

app.use( require('request-param')({ order: ["query","body","sessData"] } ) )

Inject exceptional order on the fly

if you want to override the order given to the middleware constructor specifically for one place - give your exceptional order as 3rd parameter

app.post( '/some/:value', function(req,res,next) {
    req.param('value', 'dff', ["query","sessData","cfg"] ); // works too!
})

Note

This middleware is an abstraction layer that hides the actual source from which parameters are retrieved.

The benefits of it is - that you don't really care. You can change the protocol as you like, and move parameters from path to query, from get to POST - the handler will not care - it will just work, for the cost of few CPU cycles spent on checks.

Do mind the pitfal: if you always take your parameters from the sane single source, for example - always from request.params (path parameters), or from request.query (query-string) there is no real motive to use this abstraction layer, and spend these CPU cycles needlessly...

install

npm install request-param

specifications

you can produce the current specs by running the test suite

  request-param
    √ should be a middleware factory, names 1 argument - cfg
    using the factory without arguments
      √ should not fail
      √ should return a middleware handler
      using the middelware with valid arguments
        √ should not fail and call the done
        √ should decorate the request with a method 'param', names 3 args: field, default, order
        using req.param(..) api
          √ should search by default order(first in 'params', then in 'body', then in 'query' and last - return defaultValue)
          √ should stop on the first collection that has the searched field
          with injected order
            √ should search the collections by the injeted order, ending with defaultValue
    using the factory with settings object with 'order' attribute as valid array of strings
      √ should not fail
      √ should return a middleware handler
      using the middleware with valid arguments
        √ should not fail and call the done
        √ should decorate the request with a method 'param', names 3 args: field, default, order
        using req.param(..) api
          √ should search the collections by the order they are given
          √ should stop on the first collection that has the searched field
          with in-call injected order
            √ should search the collections by the injeted order, ending with defaultValue

  15 passing (71 ms)

contribute

  1. Add specs / tests
  2. Change code
  3. Pass unit-tests
  4. Send PR :)

lisence

MIT

last word

have fun :)