JSPM

corps

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 41
  • Score
    100M100P100Q51938F
  • License MIT

Streaming http body parser

Package Exports

  • corps

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (corps) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Corps

Streaming HTTP request body parser

npm install corps

Usage

var body = require('corps');

http.createServer(function(req, res) {
    body.auto(req).map(function(data) {
        res.end(data.greeting + "world");
    });
}).listen(3000);
$ curl -X POST -D '{"greeting": "salutations"}' -H 'Content-type: application/json' localhost:8000

salutations world

$ curl -X POST -D 'greeting=what+ho' -H 'Content-type: application/x-www-form-urlencoded' localhost:8000

what ho world

API

auto :: Request → Stream Params

Sniffs Content-type and parses the request body with the appropriate parser. Knows about application/json and application/x-www-form-urlencoded to begin with. Everything else is left as raw strings. To teach it about other formats, add to mimeParsers.

json, query, raw

Individual parsers that comprise auto. Use one of these if you know what format the data will be in.

mimeParsers :: Map ContentType (Request → Stream Params)

A map of Content-types to parsers. A parser is a function that takes a request and returns a stream containing a single parameters object.

bodyParams :: (String → Stream Params) → Request → Stream Params

Lets a parser that works on strings work with request streams.

`handleError :: (String → Params) → String → Stream Params

Wraps a simple parser (that could throw errors) such as JSON.parse in a stream that handles exceptions.

Licence

MIT.