-
-
Save pankajpatel/16c1f09f6475816b187ff7498f24a00d to your computer and use it in GitHub Desktop.
Revisions
-
J. Voigt revised this gist
Sep 10, 2014 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,3 +18,11 @@ Then ... --form lastName=Mooney \ http://localhost:8080/submit ## See also Other `multipart/form-data` demos: * [hapi](https://github.com/joyrexus/multipart-demo) - serving various upload clients * [http](https://github.com/Raynos/http-framework/blob/master/examples/multipart/server.js) - sans framework * [express](https://github.com/strongloop/express/blob/master/examples/multipart/index.js) -
J. Voigt revised this gist
Sep 10, 2014 . 1 changed file with 2 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,26 +20,17 @@ server.route({ var name = data.file.hapi.filename; var path = __dirname + "/uploads/" + name; var file = fs.createWriteStream(path); file.on('error', function (err) { console.error(err) }); data.file.pipe(file); data.file.on('end', function (err) { var ret = { filename: data.file.hapi.filename, headers: data.file.hapi.headers } reply(JSON.stringify(ret)); }) -
J. Voigt revised this gist
Sep 10, 2014 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ { "name": "file-upload-demo", "version": "0.0.0", "description": "multipart form/file upload demo with hapi.js", "main": "server.js", "scripts": { "setup": "[ -d uploads ] || mkdir uploads", @@ -12,6 +12,8 @@ "multipart", "upload", "file", "form", "hapi", "demo" ], "author": "J. Voigt", -
J. Voigt created this gist
Sep 10, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ Demo of multipart form/file uploading with `hapi.js`. ## Usage npm install npm run setup npm run server Then ... npm run test ... or try: curl --form [email protected] \ --form firstName=Melvin \ --form lastName=Mooney \ http://localhost:8080/submit This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ A,B,C a,b,c 1,2,3 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ { "name": "file-upload", "version": "0.0.0", "description": "multipart file upload demo", "main": "server.js", "scripts": { "setup": "[ -d uploads ] || mkdir uploads", "test": "curl --form [email protected] --form user=Jones http://localhost:8080/submit", "server": "node server.js" }, "keywords": [ "multipart", "upload", "file", "demo" ], "author": "J. Voigt", "license": "BSD-2-Clause", "dependencies": { "hapi": "~6.8.0" } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ var fs = require('fs'); var Hapi = require('hapi'); var server = Hapi.createServer('localhost', Number(process.argv[2] || 8080)); server.route({ method: 'POST', path: '/submit', config: { payload: { output: 'stream', parse: true, allow: 'multipart/form-data' }, handler: function (request, reply) { var data = request.payload; if (data.file) { var name = data.file.hapi.filename; var path = __dirname + "/uploads/" + name; var file = fs.createWriteStream(path); var body = ''; file.on('error', function (err) { console.error(err) }); data.file.pipe(file); data.file.on('data', function (data) { body += data }) data.file.on('end', function (err) { var ret = { description: data.file.description, file: { data: body, filename: data.file.hapi.filename, headers: data.file.hapi.headers } } reply(JSON.stringify(ret)); }) } } } }); server.start(function () { console.log('info', 'Server running at: ' + server.info.uri); });