New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

form-parser

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

form-parser

A streaming and asynchronous multipart/form-data parser.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Description

A streaming and asynchronous multipart/form-data parser.

Install

npm install form-parser --save

Examples

Using micro:

// Dependencies
const parser = require('form-parser')
const { send } = require('micro')

// Create server
module.exports = async (req, res) => {
  // Parse request
  await parser(req, async field => {
    // Log info
    console.log(field) // { fieldType, fieldName, fieldContent }
  })

  // Reply with finished
  return send(res, 200, 'Parsing form succeded.')
}

Native HTTP server:

// Dependencies
const http = require('http')
const parser = require('form-parser')

// Create server
const server = http.createServer(async (req, res) => {
  // Wrap in try/catch block
  try {
    // Parse request
    await parser(req, async field => {
      // Log info
      console.log(field) // { fieldType, fieldName, fieldContent }
    })
  
  // Catch errors
  } catch (err) {
    // Log error
    console.error(err)

    // Reply with error
    res.statusCode = 400
    return res.end('Something went wrong.')
  }

  // Reply with success
  return res.end('Parsing form succeded.')
})

// Start server
server.listen(3000, () => {
  console.log('Listening on port 3000...')
})

Streaming file upload:

// Dependencies
const http = require('http')
const parser = require('form-parser')
const path = require('path')
const fs = require('fs')

// Create server
const server = http.createServer(async (req, res) => {
  try {
    // Parse request
    await parser(req, async field => {
      // Get info
      const { fieldType, fieldName, fieldContent } = field
      
      // Only handle files
      if (fieldType !== 'file') {
        return
      }

      // Get file info
      const { fileName, fileType, fileStream } = fieldContent

      // Prepare write stream
      const writeFilePath = path.resolve(__dirname, 'files', fileName)
      const writeFileStream = fs.createWriteStream(writeFilePath)

      // Write file to disk
      await new Promise((resolve, reject) => {
        fileStream.pipe(writeFileStream).on('error', reject).on('finish', resolve)
      })

      // Log info
      console.log(`${fileName} has been written to disk.`)
    })
  
  // Catch errors
  } catch (err) {
    // Log error
    console.error(err)

    // Reply with error
    res.statusCode = 400
    return res.end('Something went wrong.')
  }

  // Reply with success
  return res.end('Parsing form succeded.')
})

// Start server
server.listen(3000, () => {
  console.log('Listening on port 3000...')
})

API

parser(req, callback)

The parser() function is a top-level function exported by the form-parser module.

  • req HTTP request object.
  • callback(field => {}) An Async function, that's called for each new form field found. Passes field as argument.

field Is an object containing the following keys:

  • fieldType The field type (one of 'text' or 'file').
  • fieldName The field name.
  • fieldContent The field content.
    • If fieldType is 'text', fieldContent will contain the field text value.
    • If fieldType is 'file', fieldContent will contain an object with the following keys:
      • fileName The name of the file.
      • fileType The mime type of the file.
      • fileStream The file stream (ReadableStream).

Keywords

FAQs

Package last updated on 31 Mar 2018

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