Simple http file store in node.js
Storehouse is a small, simple node.js module that allows you to easily handle HTTP file uploads. It also comes with a convenient command line utility for creating a standalone Storehouse server.
Storehouse requires node.js and npm.
You can install Storehouse for use in your own project:
npm install storehouse
Or you can install Storehouse globally, making it easy to run the standalone server:
sudo npm install storehouse -g
If you're already using express, you can attach a storehouse directly to your app:
var Storehouse = require 'storehouse' ;var storehouse =url: '/fileupload'directory: './files'allowDownload: truedownloadPrefix: '/files'secret: 'this is the secret key';storehouseattach app ; // attach to an existing express app
If you don't already have an express app, you can tell storehouse to listen on its own:
Storehouse also supports SSL:
storehouselistenport: 8888ssl:key: './path/to/ssl.key'cert: './path/to/ssl.crt'port: 4443;
Usage: storehouse [options]Options:-h, --help output usage information-s, --secret <secret key> Specify the secret key for the storehouse. !!REQUIRED!!--nooverwrite Do not allow files to be overwritten.--url <url> Specify the upload url. Eg: --url "/uploadfile" Default: /upload-d, --directory <path> Specify the location to store files. Eg: --directory ./files Default: ./--allowDownload Allow file downloads. Default: off--prefix <prefix> Specify the prefix for downloading files. Eg: --prefix /files Default: /-p, --port <port> Specify the port to listen on. Default: 8888--sslkey <keyfile> Specify an SSL key file.--sslcert <certfile> Specify an SSL cert file.--quiet Do not print out upload events.
storehouse -s "this is the secret key" --allowDownload --url /testupload --nooverwrite
This would start a Storehouse server with the secret key "this is the secret key" that:
That's where the secret key comes in: to upload you must send a signature along with the file which Storehouse will validate.
The signature is a SHA1 of the sorted key/value pairs in your request, plus a secret key:
var verification = '';for var key in Objectkeys opts sortverification += key + '=' + opts key + '&';verification += 'secret=this is the secret key';var signature = CryptoJSSHA1 verification ;
The signature you send with the file must match this signature composed from the sorted key/value pairs of your request body plus the secret key.
Good question! Storehouse is mostly intended to be used as a part of an existing web infrastructure where you already have some kind of web service running.
In that service, you should expose a way for a user to obtain a signature for a file they'd like to upload. In that case, you can verify they have permission to upload and you can keep your secret key secret. Here's an example of how you might usually handle a file upload in this way:
ajaxCallurl: '/api/fileuploadsignature'type: 'POST'data:path: '/some/path/to/upload/to.png'type: 'image/png'// here your API has given us back a signature that allows this file to be uploaded,// now we can send the file to the servervar formData = ;formDataappend 'path' path ;formDataappend 'file' file ; // this would be from a file input in a form, for exampleformDataappend 'signature' signature ;var xhr = ;if xhrreadyState == 4 // completeif xhrstatus < 200 || xhrstatus >= 400alert xhrresponseText ; // oops, error!xhruploadaddEventListener 'progress'if progressEventlengthComputablevar percentComplete = Mathfloor progressEventloaded / progressEventtotal * 100 ;console.log percentComplete ; // let's print the progress of our upload to the consolefalse ;xhraddEventListener 'load'alert 'Done!' ;false ;xhraddEventListener 'error'alert error ;false ;xhraddEventListener 'abort'alert 'Aborted!' ;false ;xhropen 'POST' '/fileupload' true ; // open a post to whatever URL you've configured Storehouse to listen toxhrsend formData ; // send the file;
I created this because I became frustrated working with Amazon S3/CloudFront. Don't get me wrong, S3/CloudFront is great: tough to beat on price and there's no question of it handling scaling.
So why was I frustrated? Because I am often a 1-man team. Amazon AWS services are great, but they're really meant for larger-scale operations. Sometimes you just need to upload some files and not have to try to figure out all the nooks and crannies that AWS provides for managing a huge enterprise. And Amazon's approach is essentially that you write your own tooling.
Check out this great post by Vikrum Nijjar about switching from S3 to Fastly: https://www.firebase.com/blog/2012-07-30-making-the-switch-from-amazon-cloudfront-to-fastly.html
That post started me down this road. Except I needed a way for users to upload things to my server that I could then allow Fastly to cache. Hence: Storehouse.