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

form-parser

v0.1.1

Published

A streaming and asynchronous multipart/form-data parser.

Downloads

324

Readme

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).