Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/passabilities/num-parser
Parse numbers in ExpressJS requests.
https://github.com/passabilities/num-parser
expressjs javascript middleware nodejs number parser
Last synced: 18 days ago
JSON representation
Parse numbers in ExpressJS requests.
- Host: GitHub
- URL: https://github.com/passabilities/num-parser
- Owner: passabilities
- License: mit
- Created: 2017-06-02T17:19:17.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-16T18:58:48.000Z (over 6 years ago)
- Last Synced: 2024-10-11T06:43:06.172Z (about 1 month ago)
- Topics: expressjs, javascript, middleware, nodejs, number, parser
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# num-parser [![npm](https://img.shields.io/npm/v/num-parser.svg)](https://www.npmjs.com/package/num-parser)
Parse numbers in ExpressJS requests.
## Installation
npm i num-parser --save
## Usage
The module will parse all numbers in `req.query`, `req.params`, and `req.body`.
Also works with floating points.
To work with `req.body` you need to add after [body-parser](https://github.com/expressjs/body-parser):
```javascript
const bodyParser = require('body-parser')
const numParser = require('num-parser')app.use(bodyParser.json())
app.use(numParser)// ?a=1&b[c]=tw0&b[d]=3.5
app.use('/', (req, res) => {
console.log(req.query)
// => { a: 1, b: { c: 'tw0', d: 3.5 } }
})
```NOTE: I've had problems having it as a global middleware to work with `req.params`. If it's not working for you, try putting the middleware in for each route:
```javascript
// ?a=1&b[c]=tw0&b[d]=3.5
app.use('/', numParser, (req, res) => {
console.log(req.query)
// => { a: 1, b: { c: 'tw0', d: 3.5 } }
})
```