Coder Social home page Coder Social logo

express-param's Introduction

Express-param

Fetch Express.js Request parameters Middleware

About

You can reduce count of code line. It can remove redundant code and generate highly readability.

See below.

  • may be existing
	function route(req, res, next) {
    	var id = req.params.id
        , count = req.param('count') ? parseInt(req.param('count'), 10) : 10
        , odrer = req.param('order')
        , type = req.param('type')
        , name = req.param('name')
        , since = req.param('since')
        , from = req.param('from')
        , res_id = req.param('res_id');

        if (!res_id) return next(400);
        if (!name) return next(400);
        if (!order) order = 'desc';
        if (!type) type = 'integer';

        // some code...
    }
  • after code
	function route(req, res, next) {
    	var requiredParams = ['{id}', 'name', 'res_id']
        , optionalParams = ['number:count|=10', 'order|=desc', 'type|=integer', 'since', 'from']
        , options;

        options = req.fetchParamter(requiredParams, optionalParams);

    	if (req.checkParamErr(options)) return next(options);

		return res.send(options);
    }

parameter syntax

I was inspired by Spring Framework and Flask.

  • required parameter
    • Array
    • [element1, element2, element3, ...]
    • type:{parameter_name}
      • type: type is optional(default string). number or string. return variable with type.
      • {paremeter_name}: {} is optional. but paremeter name is required. if it include {}, it means that this is path variable
      • ex) ['number:{flitto_id}], 'string:req_id', 'res_id']
  • optional parameter
    • Array
    • [element1, element2, element3, ...]
    • type:parameter_name|=default_value
      • type: type is optional(default string). number or string. return variable with type.
      • parameter_name: required.
      • default_value: optional. if request object do not have specified parameter then assign this default value.
      • ex) ['number:count', 'order', 'string:res_list|=Y']

Example

Here is an simple example.

var express = require('express');
var fetcher = require('express-param');
var app = express();
app.use(fetcher());

app.get('/path', function(req, res, next) {
	var requiredParams = ['id'];
 var optionalParams = ['count'];
	var options = req.fetchParamter(requiredParams, optionalParams);

 if (req.checkParamErr(options)) return next(options);

 return res.send(options);
});

Here is another example with express-param syntax

var fetcher = require('express-param');
var app = express();
app.use(fetcher());

app.get('/path/:id/', function(req, res, next) {
	var requiredParams = ['string:{id}'];
 var optionalParams = ['number:count|=10, order|=desc'];
	var options = req.fetchParamter(requiredParams, optionalParams);

 if (req.checkParamErr(options)) return next(options);

 //options values is below.
 /*
 	{
     	id: '10',
         count: 10
         order: 'desc'
     }
 */
 return res.send(options);
});

Another example

Custom reqeust key name belong to express req property

var express = require('express');
var fetcher = require('express-param');
var app = express();
app.use(fetcher({
 'ipaddr': 'ip'
}));

app.get('/path', function(req, res, next) {
	var requiredParams = ['id'];
 var optionalParams = ['count', 'ipaddr'];
	var options = req.fetchParamter(requiredParams, optionalParams);

 if (options.err) return next(options.err);

 /*
     options.ipaddr is equal to req.ip
 */
 return res.send(options.params);
});

API

fetchParameter(required[, optional])

fetch parameter of required and optional

Add ON

fetch geographic information

It can fetch country information from remote address!

 var addOnOpt = {
     geoip: {
         keyName: 'headers.x-forwarded-for'
     }
 };
 app.use(fetcher({
     'ipaddr': 'ip'
 }, addOnOpt));
 
 ////// ....
 
 console.log(req.param('x-fetcher-geoinfo'))
 //// output
      { country:
        { country_name: 'United States',
          country_code: 'US',
          country_code3: 'USA',
          continent_code: 'NA' } 
      }  
 ///

fetch detail imsi information by mnc, mcc code

It can generate detail imsi(coutnry, operator...) informtaion by mcc, mnc doe!

 var addOnOpt = {
     ismi: true
 };
 app.use(fetcher({
     'ipaddr': 'ip'
 }, addOnOpt));
 
 ////// ....
 
 // url maybe hostname/api?mnc=11&mcc=450. if only exist mcc then results array length may be greater than 1.
 
 console.log(req.param('x-imsi'))
 //// output
[ { country_name: 'South Korea',
    country_code: 'KR',
    mcc: '450',
    mnc: '11',
    brand: 'SKTelecom',
    operator: 'Korea Cable Telecom(t-plus), Eco-mobile',
    status: 'Operational',
    bands: 'UMTS 2100' } ] }]
 ///

LICENSE

MIT


This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.