Code Monkey home page Code Monkey logo

spirit-body's Introduction

spirit-body

Body parser for spirit.

Supports Raw, JSON and URL-encoded bodies.

Slack Build Status Coverage Status NPM Downloads NPM Dependencies

Usage

Add as spirit middleware:

import body from 'spirit-body'

// tell spirit-body to parse JSON
const jsonBody = body({
	json: true
})

// add a spirit route
route.wrap(
	route.post(
		'*',
		['body'],
		body => `the body is ${JSON.stringify(body)}`
	),
	[jsonBody]
)

Errors

If body parsing failed spirit-body will set request.invalidBody to true. If options.error is true spirit-body will automatically respond with 400 Bad Request. Otherwise request.body will be undefined.

Options

Name Description Values
allowEmptyBody ignores requests without content-length true, false
error enable 400 Bad Request responses false, true
form enable form-parsing false, true
json enable json-parsing false, true
text enable text-parsing false, true
limit body size limit 4*1024*1024, 1024, ...

Install

With npm installed, run

npm install --save spirit-body

or use yarn:

yarn add spirit-body

See Also

License

MIT

spirit-body's People

Contributors

dependabot-preview[bot] avatar florianwendelborn avatar fvj avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

spirit-body's Issues

Gracefully handle failure case of JSON.parse

It seems that providing invalid JSON to the parser would trigger an error but the error would not be reported correctly to the parent handler.

Maybe I missed something but it might be worth adding a try / catch around the JSON parser and rejecting the promise in the event of a failure. Ideally to keep in the spirit of spirit the error should just be returned so that a parent error handler could cleanly report the error.

Is this issue present and if so would you accept a pull request to add the handling and the relevant test case?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.