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

formidable-upload

v0.0.3

Published

Chainable file upload api for express using formidable

Downloads

3

Readme

Formidable Upload

Chainable file upload api for express using formidable.

Installation

$ npm install formidable-upload

Usage

var upload = require('formidable-upload');

// ..

upload()
    .accept('image/jpeg')
    .to(['public', 'images'])
    .resize({
        use: 'resize',
        settings: {
            width: 800,
            quality: 80
        }
    })
    .imguri()
    .exec(req.files.displayImage, function(err, file) {
        // further process file
    });

Processing Chains

After creating an Upload object by calling upload() the upload object exposes API methods

accept

Restricts files accepted by upload to specified mime types. Mime types can be specified as strings, array of strings, regex or array of regex. In case match fails an err object is passed to the exec callback field accept set to true.

Examples

Accept mp4, ogg and jpeg files in upload

  accept(['audio/mp4', 'audio/ogg', 'image/jpeg'])

Accept only mp4

  accept('audio/mp4')

Accept all image mime types

  accept([/image*/])

to

Moves files from temp express upload location to the target location. The to processor modifies file.path and file.name fields of the original express upload file to point to the moved upload file. to will use any given file extension from the original file.name. A target directory is expected as argument, in case an array of strings is passed this array will be joined via path.join.

Examples

Moves uploaded files to public/images

  to(['public', 'images'])

Moves uploaded files to public/images

  to('public/images')

resize

Allows uploaded files to be processed via Magickwand. See magickwand page for using magickwand. Magicwand supports resize and thumbnail methods. Please refer the magickwand page if you face any issue while installing the module.

Examples

Resize an uploaded image to width of 800 and quality of 80 maintaining the aspect ratio.

  resize({
      use: 'resize',
      settings: {
          width: 800,
          quality: 80
      }
  })

imguri

Convert the uploaded image to data uri, sets req.files.userfile.data with Base64 encoded data.

  imguri()

process

Generic handler to pass custom transformation code in the processing chain. Functions passed to process must have the signature

  fn(file, cb)

Where file is the processed upload file and cb is the callback.

exec

Executes a processing chain. Processing chains are reusable and may be executed multiple times. In case the file is not part of the upload or is empty an err is passed to the callback with a field noFile set to true.

Middleware

Instead of executing and managing upload file processing in your routes, express-upload can be used as middleware in express. Any processing errors (no file, file type not accepted,...) are stored in a err property of the file to upload.

Example

  // Build an upload instance but don't execute it right now
  var uploader = upload()
      .accept('image/jpeg')
      .to(['public', 'images'])
      .resize({
          use: 'resize',
          settings: {
              width: 800,
              quality: 80
          }
      })
      .imguri();


  // Define a middleware for handling image upload
  app.post('/upload', uploader.middleware('userfile'), routes.upload);

In case an error would occur when uploading the next middleware will be called with the error.

Example Code

Formidable Upload Example.