Socket
Socket
Sign inDemoInstall

@hapi/subtext

Package Overview
Dependencies
10
Maintainers
7
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @hapi/subtext

HTTP payload parsing


Version published
Weekly downloads
814K
decreased by-15.56%
Maintainers
7
Created
Weekly downloads
 

Package description

What is @hapi/subtext?

@hapi/subtext is a module for parsing request payloads in Hapi.js applications. It supports various content types including JSON, form data, and multipart data. It is designed to handle large payloads efficiently and provides a simple API for extracting and processing request data.

What are @hapi/subtext's main functionalities?

Parsing JSON Payloads

This code demonstrates how to use @hapi/subtext to parse JSON payloads from incoming HTTP requests. The `Subtext.parse` method is used to extract and parse the payload, which is then sent back in the response.

const Subtext = require('@hapi/subtext');
const Http = require('http');

const server = Http.createServer(async (req, res) => {
  const { payload } = await Subtext.parse(req, null, { parse: true, output: 'data' });
  res.end(`Received JSON: ${JSON.stringify(payload)}`);
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Parsing Form Data

This example shows how to parse form data using @hapi/subtext. The `Subtext.parse` method is used similarly to the JSON example, but it can handle form-encoded data as well.

const Subtext = require('@hapi/subtext');
const Http = require('http');

const server = Http.createServer(async (req, res) => {
  const { payload } = await Subtext.parse(req, null, { parse: true, output: 'data' });
  res.end(`Received Form Data: ${JSON.stringify(payload)}`);
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Handling Multipart Data

This code demonstrates how to handle multipart data using @hapi/subtext. The `Subtext.parse` method is configured to output a stream, which is suitable for handling large multipart payloads.

const Subtext = require('@hapi/subtext');
const Http = require('http');

const server = Http.createServer(async (req, res) => {
  const { payload } = await Subtext.parse(req, null, { parse: true, output: 'stream' });
  res.end('Received Multipart Data');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Other packages similar to @hapi/subtext

Readme

Source

@hapi/subtext

HTTP payload parser.

subtext is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi – they work even better together.

Visit the hapi.dev Developer Portal for tutorials, documentation, and support

Useful resources

Keywords

FAQs

Last updated on 14 Feb 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc