Last active
October 19, 2016 14:21
-
-
Save nvcexploder/053b4b28429ef9675546 to your computer and use it in GitHub Desktop.
Streaming payloads with Hapi
This file contains 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 characters
$ npm install hapi joi | |
#A good way to test is this: | |
$ curl --form [email protected] localhost:8080/selfies |
This file contains 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 characters
var Hapi = require('hapi'); | |
var Fs = require('fs'); | |
var Joi = require('joi'); | |
var server = new Hapi.Server(8080); | |
var routeConfig = { | |
validate: { | |
payload: { | |
file: Joi.string().required().description('file name for picture upload') | |
} | |
}, | |
payload: { | |
output: 'stream', | |
parse: true, | |
allow: 'multipart/form-data' | |
}, | |
handler: function (request, reply) { | |
//create whatever writestream you want | |
var image = Fs.createWriteStream(request.payload.file.hapi.filename); | |
image.on('finish', function () { | |
reply({status: 'ok!'}); | |
}); | |
request.payload.file.pipe(image); | |
} | |
} | |
server.route({ method: 'POST', path: '/selfies', config: routeConfig }); | |
server.start(function (err) { | |
console.log('oh word?'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment