0.1.5 • Published 7 years ago

urlencode2json-middleware v0.1.5

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

Build Status Coverage Status

Installation

$ npm install urlencode2json-middleware

Usage

urlencode2json-middleware is a middleware to enhance express bodyparser to parse string to crrect json type.

Example app.js:

const express = require('express')
const bodyParser = require('body-parser')
const urlencode2json = require('urlencode2json-middleware')

const app = express()
app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
app.use(urlencode2json)

When a post request as below send to server

POST / HTTP/1.1
Host: ******
Content-Type: application/x-www-form-urlencoded

user=tobi&arg_bool=true&arg_number=3&arg_bool_str="true"&arg_number_str="33"&arg_null=null&arg_null_str="null"

By default, bodyParser.urlencoded can not distinguish number, boolean, null with string, and will parse data to request.body like below:

request.body === {
  "user":"tobi",
  "arg_bool":"true",
  "arg_number":"3",
  "arg_bool_str":"true",
  "arg_number_str":"33",
  "arg_null":"null",
  "arg_null_str":"null",
}

This middleware will enhance bodyParser.urlencoded to paser default to correct primary type, and will paser data to request.body like below:

request.body === {
  "user":"tobi",
  "arg_bool":true,
  "arg_number":3,
  "arg_bool_str":"true",
  "arg_number_str":"33",
  "arg_null":null,
  "arg_null_str":"null",
}
0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago