Skip to content
Connect middleware to convert parameter values to native types
CoffeeScript JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
LICENSE.txt
README.md
index.js
package.json

README.md

connect-typed-body

Connect middleware to convert parameter values to native types

Build Status

It relies on req.body parsed by connect.bodyParser.

"3" -> 3
"3.5" -> 3.5 # decimal point is localizable
"3,545" -> 3545 # thousands seperator is localizable
"false" -> false
"true" -> true
"null" -> null
"undefined" -> undefined
"" -> undefined

Usage

Defaults (thousands_seperator = ',', decimal_point = '.')

var connect = require('connect');
var typedBody = require('connect-typed-body');
var app = connect();
app.use(connect.bodyParser);  // important must come before typedBody
app.use(typedBody());
app.use( function(req, res){
  res.end JSON.stringify(req.body)
}); 

Localized

var connect = require('connect');
var typedBody = require('connect-typed-body');
var app = connect();
app.use(connect.bodyParser);  // important must come before typedBody
app.use(typedBody({thousands_seperator: '.', decimal_point: ','}));
app.use( function(req, res){
  res.end JSON.stringify(req.body)
}); 

Dynamic overwrite

var connect = require('connect');
var typedBody = require('connect-typed-body');
var app = connect();
app.use(connect.bodyParser);  // important must come before typedBody
app.use( function(req, res,next){
  if(req.locale == "de_DE"){
    req.decimal_point = ',' ;
    req.thousands_seperator = '.';
  }
  next(); 
});
app.use(typedBody());
app.use( function(req, res){
  res.end JSON.stringify(req.body)
}); 

Copyright

Copyright (c) 2012 Marc Rene Arns. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.