GithubHelp home page GithubHelp logo

isabella232 / node-middleware-json-parse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tabcorp/node-middleware-json-parse

0.0 0.0 0.0 7 KB

JSON parsing middleware

License: MIT License

JavaScript 100.00%

node-middleware-json-parse's Introduction

middleware-json-parse stability

npm version build status test coverage downloads js-standard-style

JSON parsing middleware.

Features:

  • fast
  • works with any framework
  • flexible
  • safe (e.g. will never crash your server)

Installation

$ npm install middleware-json-parse

Usage

const parseJson = require('middleware-json-parse')
const middleware = require('http-middleware')
const http = require('http')

http.createServer((req, res) => {
  const mw = [ parseJson(req, 'body') ]
  middleware(req, res, mw, (err) => {
    res.statusCode = err ? 500 : 200
    console.log('body', req.body)
    res.end()
  })
}).listen()

API

parser = parseJson(ctx, propName, errFn?)

Create safe json parsing middleware. Requires a context object on which to set a propName containing the body value (in express: req, 'body'). Takes an optional function as the third argument to format errors that are passed to next. Errors are by default formatted as new Error({ message: 'Invalid JSON' }).

parser(req, res, next?)

Safely parse JSON data from req, mount it on the context as propName, and call next when done. Only parses content if the Content-Type=application/json HTTP header is set.

License

MIT

node-middleware-json-parse's People

Contributors

rprieto avatar yoshuawuyts avatar

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.