Skip to content

Instantly share code, notes, and snippets.

@nvcexploder
Last active October 19, 2016 14:21
Show Gist options
  • Save nvcexploder/053b4b28429ef9675546 to your computer and use it in GitHub Desktop.
Save nvcexploder/053b4b28429ef9675546 to your computer and use it in GitHub Desktop.
Streaming payloads with Hapi
$ npm install hapi joi
#A good way to test is this:
$ curl --form [email protected] localhost:8080/selfies
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