Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timneutkens/raw-text
Parse raw request body as text
https://github.com/timneutkens/raw-text
Last synced: 21 days ago
JSON representation
Parse raw request body as text
- Host: GitHub
- URL: https://github.com/timneutkens/raw-text
- Owner: timneutkens
- Created: 2017-01-03T19:39:39.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-08T20:17:26.000Z (about 8 years ago)
- Last Synced: 2024-04-14T09:58:28.872Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Raw Text
Parse raw request body as text
## Installation
`npm install --save raw-text`
## Usage
Using [Micro](https://www.github.com/zeit/micro)
```js
const parse = require('raw-text')module.exports = async function (req, res) {
const text = await parse(req)
console.log(text)return ''
}
```Using the built in HTTP server provided by node.js
```js
const http = require('http');
const parse = require('raw-text')const server = http.createServer((req, res) => {
const text = parse(req).then(text => {
console.log(text)
res.end()
})
})server.listen(8000)
```### Api
`parse(req, {limit = '1mb'})`
- Use `require('raw-text')`
- Returns a `Promise` (can be used with async/await)
- Buffers the incoming body, calls `toString()` and returns it.
- limit is how much data is aggregated before parsing at max. It can be a `Number` of bytes or [a string](https://www.npmjs.com/package/bytes) like `'1mb'`.
- The `Promise` is rejected when an error occurs, it is your responsibility to catch it.#### Background
Requested feature for [Micro](https://www.github.com/zeit/micro) in pull request [116](https://github.com/zeit/micro/pull/116) and [118](https://github.com/zeit/micro/pull/118).
#### Credits to
[Jeremy Morrell](https://github.com/jmorrell) and [Sam Garson](https://github.com/samtgarson) for explaining the need for this package.