use-body
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

use-body NPM version NPM monthly downloads NPM total downloads

Extract the request body from JSON, text and urlencoded formats.

Install

Install with npm:

$ npm install use-body

Usage

import http from "node:http";
import useBody from "use-body";

// TYPES: json / text / urlencoded

// JSON example

http
  .createServer((req, res) => {
    useBody(
      req,
      res,
      () => {
        console.log("Request body", req.body);
        res.end("Working!!!");
      },
      "json"
    );
  }) // Default format always will be json, but you can choose between json, text and urlencoded as formats
  .listen(3000);

/*
    example result: 
    Request body { email: 'body@usebody.com' }
*/

// Text example

http
  .createServer((req, res) => {
    useBody(
      req,
      res,
      () => {
        console.log("Request body", req.body);
        res.end("Working!!!");
      },
      "text"
    );
  }) // Default format always will be json, but you can choose between json, text and urlencoded as formats
  .listen(3000);

/*
    example result: 
    Request body: some text 
*/

// URL encoded example

http
  .createServer((req, res) => {
    useBody(
      req,
      res,
      () => {
        console.log("Request body", req.body);
        res.end("Working!!!");
      },
      "urlencoded"
    );
  }) // Default format always will be json, but you can choose between json, text and urlencoded as formats
  .listen(3000);

/*
    example result: 
    Request body: { user: 'getbody' }
*/

Author

Otavio Rampinelli

License

Copyright © 2024, Otavio Rampinelli. Released under the MIT License.


Package Sidebar

Install

npm i use-body

Weekly Downloads

5

Version

1.0.0

License

MIT

Unpacked Size

11.2 kB

Total Files

11

Last publish

Collaborators

  • otarampinelli