node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey »


What it does

Easy way to attach documents to objects


Because there is no easy to save attached files in a hierarchical way that is simple to use.

How to use it

First install it

npm install -g stapler

var stapler = require('stapler'); // Require the module 
var story = Story.findById('123'); // Get the record you want to attach the file to 
var file = { path: 'tmp/1298hasdk3hfskjhsdf.file', name: 'sunset.jpg' }; // Normally will be from req.files 
var config = { collection: 'story', id: } // Collection and id are required 
// Will move the file from the tmp location and into './public/uploads/#{collection_name}/#{id}/#{filename}' 
stapler.attach(file, config, function (err, new_file) {; // 'sunset.jpg' 
  new_file.path; // './public/uploads/story/123/sunset.jpg'; 
  // Save the path to the record for future use. 
  story.photo_path = new_file.path;
  story.photo_name =;;

Then in your views you can access it like this

<img src="/uploads/story/#{}/#{story.photo_name}">


Collection attachment does not do ANYTHING to the model. You must handle that yourself. All that you should need to do is save the filename to your object in Mongo, MySQL or whatever datastore. Then in your view you can easily construct the file path as shown above.

Right now if two files are upload with the same name to the same record the old one will be overwritten


  • Save files by with timestamp and filename hash so that you can upload multiple files with the same name


In the command line run mocha test