Skip to content

Instantly share code, notes, and snippets.

@bewest
Last active December 20, 2015 16:09
Show Gist options
  • Save bewest/6159923 to your computer and use it in GitHub Desktop.
Save bewest/6159923 to your computer and use it in GitHub Desktop.
use node restify to handle mime body uploads

Use node to handle file upload

Edit
# https://gist.github.com/6159923.git
[email protected]:6159923.git

git clone $gist blip_upload
# OR
git remote add origin $gist

#####
# EOF
node_modules/
.*.sw?

Use node to handle file upload

How to handle file uploads with node?

Demo

Notice, there is a req.files attribute, which is a hash/object. Each value is also a hash, wich a path property indicating the name and location of the uploaded file.

Works with curl uploading mechanics.

XXXX UPLOADED FILES { profile:
   { domain: { members: [Object], _events: [Object] },
     size: 243,
     path: '/tmp/cb968f682765196bc42e71e2b05af112',
     name: 'profile.json',
     type: 'application/json',
     hash: null,
     lastModifiedDate: Mon Aug 05 2013 15:04:58 GMT-0700 (PDT),
     _writeStream:
      { domain: [Object],
        path: '/tmp/cb968f682765196bc42e71e2b05af112',
        fd: 9,
        writable: false,
        flags: 'w',
        encoding: 'binary',
        mode: 438,
        bytesWritten: 243,
        busy: false,
        _queue: [],
        _open: [Function],
        drainable: true } },
  raw_data:
   { domain: { members: [Object], _events: [Object] },
     size: 825,
     path: '/tmp/74d8f3032ce669f127f21ec6d42e4005',
     name: 'raw_data.log',
     type: 'plain/text',
     hash: null,
     lastModifiedDate: Mon Aug 05 2013 15:04:58 GMT-0700 (PDT),
     _writeStream:
      { domain: [Object],
        path: '/tmp/74d8f3032ce669f127f21ec6d42e4005',
        fd: 10,
        writable: false,
        flags: 'w',
        encoding: 'binary',
        mode: 438,
        bytesWritten: 825,
        busy: false,
        _queue: [],
        _open: [Function],
        drainable: true } } }


colophon

PREREQUISITES

  • node
  • npm
  • nodemon

Once you have node and npm installed, you can do:

# install nodemon using npm
sudo npm install -g nodemon # install nodemon
#####
# EOF

from scratch

git init blip-upload
cd blip-upload
npm init
npm install --save restify
nodemon server.coffee

Works with example curl upload mechanics. using https://gist.github.com/bewest/6142358

+ /usr/local/bin/json
+ curl -v -s -X POST -H '' -F '[email protected];type=application/json' -F 'raw_data=@raw_data.log;type=plain/text' -F hello=world -F profile=profile.json -F raw=raw 'http://localhost:8085/upload/foo?raw=raw_data.log'
* About to connect() to localhost port 8085 (#0)
*   Trying 127.0.0.1... connected
> POST /upload/foo?raw=raw_data.log HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8085
> Accept: */*
> Content-Length: 1717
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=----------------------------60185bfbbf91
> 
* Done waiting for 100-continue
} [data not shown]
{
"name": "blip-upload",
"version": "0.0.0",
"description": "practice upload server handler",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "",
"license": "BSD",
"dependencies": {
"restify": "~2.6.0"
}
}
# more or less copy paste from:
# http://mcavage.me/node-restify/
restify = require('restify')
fs = require('fs')
respond = (req, res, next) ->
res.send('hello ' + req.params.name)
# http://stackoverflow.com/questions/15001822/sending-large-image-data-over-http-in-node-js
echo_upload_info = (req, res, next) ->
console.log('XXXX: BODY', req.body)
console.log('XXXX params', req.params)
console.log('XXXX UPLOADED FILES', req.files)
next( )
server = restify.createServer( )
server.get('/hello/:name', respond)
server.head('/hello/:name', respond)
server.post('/upload/:name', restify.bodyParser( ), echo_upload_info, respond)
server.get('/upload/:name', restify.bodyParser( ), echo_upload_info, respond)
server.listen((process.env.PORT || 8085), ( ) ->
console.log('%s listening at %s', server.name, server.url)
)
@bewest
Copy link
Author

bewest commented Aug 5, 2013

At @jh-bates works with https://gist.github.com/bewest/6142358

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment