Skip to content

Retrieve the request parameters from an incoming HTTP request

License

Notifications You must be signed in to change notification settings

JoeScho/get-param

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get-param

Safely retrieve the request parameters from an incoming HTTP request

Success Scenario

const { get } = require('get-param');

// Some middleware
(req, res, next) => {
  const param = get(req, 'myParam');

  // Example 1: Query Parameter
  // req = { query: { myParam: 'this is my query param' }};
  param === 'this is my query param'

  // Example 2: Body Parameter
  // req = { body: { myParam: 'this is my body param' }};
  param === 'this is my body param'

  // Example 3: Duplicate Parameter, defaults to body
  // req = { body: { myParam: 'this is my body param' }, query: { myParam: 'this is my query param' }};
  param === 'this is my body param'
}

Error Scenarios

const { get } = require('get-param');

// Some middleware
(req, res, next) => {
  const param = get(req, 'myParam');

  // Example 1: Parameter not defined
  // req = { query: { someOtherParam: 'this is my query param' }};
  param === false

  // Example 2: req not passed
  // req = undefined or null
  param === false
}

About

Retrieve the request parameters from an incoming HTTP request

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published