npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@toisu/body

v4.0.0

Published

Body parsing middleware for Toisu!

Downloads

3

Readme

toisu-body

Body parsing middleware for Toisu!

This module is a thin wrapper around the excellent body module. It appends a 'body' field to the context of your request-response chain.

Example

This server will send back JSON sent to it.

import http from 'http';
import Toisu from '@toisu/toisu';
import * as body from '@toisu/body';

const app = new Toisu();

app.use(body.json()); // Note, non-json requests will lead to an error!

app.use(function (req, res) {
  const body = this.get('body');
  const stringified = JSON.stringify(body);

  res.writeHead(200, {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(stringified)
  });

  res.write(stringified);
  res.end();
});

http.createServer(app.requestHandler).listen(3000);

Handling errors

If clients can send requests which aren't, for example, JSON, then JSON middleware will reject with an error when it attempts to parse the body. There are a few solutions to this issue:

  • Handle it in the errorHandler property of the app object.
  • Use the text middleware and parse it by hand.
  • Chain the middleware with a catch.

I recommend the third solution, which looks like:

import http from 'http';
import Toisu from '@toisu/toisu';
import * as body from '@toisu/body';

const app = new Toisu();
const json = body.json();

app.use(function (req, res) {
  return json.call(this, req, res).catch(function (err) {
    res.statusCode = 400;
    res.end();
  });
});

app.use(function (req, res) {
  const body = this.get('body');
  const stringified = JSON.stringify(body);

  res.writeHead(200, {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(stringified)
  });

  res.write(stringified);
  res.end();
});

http.createServer(app.requestHandler).listen(3000);

You could go further and check the content type of the request as well, and if the content type isn't compatible with JSON use a 415 status code.

API

This module exports four methods. Each takes a single options object which is passed on to the body module internally. See the documentation for that module for details on the options available. Each method returns a Toisu! middeware function.

text(options)

Gets the body from the request as a string, and attempts no parsing on it. Useful for when you need to do some custom checks or parsing. It appends this string to the Toisu! request-response context (this in middlewares) as 'body'.

json(options)

Gets the body from the request and parses it as JSON. It appends this string to the Toisu! request-response context (this in middlewares) as 'body'.

form(options)

Gets the body from the request and parses it as a form. It appends this string to the Toisu! request-response context (this in middlewares) as 'body'.

any(options)

Looks at the Content-Type header and will parse as either form data or JSON. If the content type doesn't conform to either, it will throw an error.