Open ap2322 opened 4 years ago
https://gist.github.com/joyrexus/0c6bd5135d7edeba7b87 -> more helpful, still getting error:
{
"statusCode": 415,
"error": "Unsupported Media Type",
"message": "Unsupported Media Type"
}
CONFIG PARSE FALSE: https://github.com/outmoded/discuss/issues/543
Maybe use multer instead??? https://medium.com/@nitinpatel_20236/image-upload-via-nodejs-server-3fe7d3faa642
There are a LOT more resources on using multer.
Concepts to explore:
read/write stream
(am able to get unparsed stream)
https://stackoverflow.com/questions/2496710/writing-files-in-node-js
First console.log gives me a stream (how to use?). The second console.log is undefined.
server.route({
path: '/upload',
method: 'POST',
options: {
payload: {
output: 'stream',
allow: "multipart/form-data",
parse: false,
maxBytes: 200 * 1000 * 1000
}
},
handler: async (req, h) => {
const { payload } = req
console.log('payload', payload)
console.log('payload.file', payload.file)
const response = handleFileUpload(payload.file)
return response
}
})
Also, with the buffer method:
// kind of works!!! Need to find out how to read buffers
server.route({
method: 'POST',
path: '/upload-data',
config: {
handler: async (request, reply) => {
const { payload } = request
console.log(payload)
const response = handleFileUpload(payload)
return response
},
payload: {
output: 'data',
allow: "multipart/form-data",
parse: false
}
}
})
Image upload from the frontend should store the image on the backend servers for future upload to twitter & 311.
https://hapi.dev/tutorials/servingfiles/?lang=en_US
https://medium.com/today-i-learned-chai/how-to-upload-files-with-node-js-hapi-js-c788b5c8586a