Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

multiparty

Package Overview
Dependencies
Maintainers
2
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multiparty

multipart/form-data parser which supports streaming

  • 4.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
459K
decreased by-20.46%
Maintainers
2
Weekly downloads
 
Created

What is multiparty?

The multiparty npm package is a node.js module for parsing multipart/form-data, which is primarily used for handling file uploads. It can parse incoming request bodies in a middleware-like fashion, making it easier to handle file uploads and form data in web applications.

What are multiparty's main functionalities?

Parsing Form Data

This feature allows you to parse form data from a POST request. The code sample demonstrates how to create an HTTP server that listens for POST requests, parses the form data using multiparty, and responds with the parsed fields and files in JSON format.

const multiparty = require('multiparty');
const http = require('http');

http.createServer((req, res) => {
  if (req.method === 'POST') {
    const form = new multiparty.Form();
    form.parse(req, (err, fields, files) => {
      if (err) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Error parsing form data');
        return;
      }
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ fields, files }));
    });
  } else {
    res.writeHead(405, { 'Content-Type': 'text/plain' });
    res.end('Method Not Allowed');
  }
}).listen(8080);

Handling File Uploads

This feature allows you to handle file uploads. The code sample demonstrates how to create an HTTP server that listens for POST requests, parses the uploaded file using multiparty, and saves it to a specified directory.

const multiparty = require('multiparty');
const http = require('http');
const fs = require('fs');

http.createServer((req, res) => {
  if (req.method === 'POST') {
    const form = new multiparty.Form();
    form.parse(req, (err, fields, files) => {
      if (err) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Error parsing form data');
        return;
      }
      const file = files.upload[0];
      const tempPath = file.path;
      const targetPath = './uploads/' + file.originalFilename;
      fs.rename(tempPath, targetPath, (err) => {
        if (err) {
          res.writeHead(500, { 'Content-Type': 'text/plain' });
          res.end('Error saving file');
          return;
        }
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('File uploaded successfully');
      });
    });
  } else {
    res.writeHead(405, { 'Content-Type': 'text/plain' });
    res.end('Method Not Allowed');
  }
}).listen(8080);

Other packages similar to multiparty

Keywords

FAQs

Package last updated on 21 Jan 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc