Socket
Socket
Sign inDemoInstall

express-paramchecker

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-paramchecker

The nodejs middleware to basically check whether all the required parameters is inside the request object.


Version published
Weekly downloads
0
Maintainers
1
Created
Weekly downloads
 

Readme

Source

express-paramchecker

The request parameter checker to be used with Express.js

Installation

Install the latest version of express-paramchecker using npm just typing npm install express-paramchecker into your terminal. Use --save directive to save the dependency to your package.json automatically.

Usage

The basic usage of express-paramchecker is pretty simple. It is used as a Express.js middleware within the route clarification. See the full example below.

Firstly create your package.json file with all the dependencies needed.

{
  "name": "Test",
  "version": "0.0.0",
  "description": "Test",
  "main": "server.js",
  "dependencies": {
    "body-parser": "^1.14.2",
    "express": "4.13.3",
    "express-paramchecker": "1.0.0"
  }
}

Then run npm install in your project root. Npm will now install all the dependencies clarified in the package.json.

Fill up your server.js with the content below. Note that this is the minimalistic usage of paramchecker with Express.js.

var express = require('express');
var app = express();
var bodyparser = require('body-parser');
var paramchecker = require('express-paramchecker');

paramchecker.on('error', function (req, res, next) {
    res.status(400).send({
        msg: 'Parameters missing!',
        success: false
    });    
});

paramchecker.on('success', function (req, res, next) {
    next();    
});

app.use(bodyparser.json());

app.post('/feedback', paramchecker.check(['name', 'email', 'content']), function (req, res, next) {
	res.status(200).send({
		msg: 'Yah!',
		success: true
	});
});

app.listen(5000);

Options

You can pass few options to ParamChecker.check(). One of those one options is the replace function. There you can declare if you want to add or remove or replace something to all input values. Hope the example below clears it out a little.

/* Just replaces all the 'cat' values in the input with 'dog'. */
var replaceFunc = function (data) {
    return data.replace('cat', 'dog');
}

var opts = {
    replace: replaceFunc
};

app.post('/feedback', paramchecker.check(['name', 'email', 'content'], opts), function (req, res, next) {
	res.status(200).send({
		msg: 'Yah!',
		success: true
	});
});

FAQs

Last updated on 03 Feb 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc