Skip to content

Instantly share code, notes, and snippets.

@tengla
Last active January 23, 2019 12:41
Show Gist options
  • Save tengla/72613eacd3fab9140fa1d17130eceaeb to your computer and use it in GitHub Desktop.
Save tengla/72613eacd3fab9140fa1d17130eceaeb to your computer and use it in GitHub Desktop.
Hapi file upload handler.
const Joi = require('joi');
module.exports = {
method: 'post',
path: '/upload',
config: {
validate: {
payload: {
files: Joi.array().items(
Joi.any()
).single().required()
}
},
payload: {
maxBytes: 1000 * 1000 * 5, // 5 Mb
output: 'file',
parse: true
},
handler: async (request, h) => {
/**
* data structure will be:
* { path: '/var/folders/39/s48jqbvx31s98m5y8x4wr_h0d74j26/T/1548246811595-6726-4ff6112d1349194b',
* bytes: 202600, filename: 'mrscruff.jpg',
* headers: { 'content-disposition': 'form-data; name="files"; filename="mrscruff.jpg"', 'content-type': 'image/jpeg' } }
*/
request.payload.files.forEach(file => {
console.log(file);
});
return { message: 'ok' };
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment