The goal is for a Node.js / hapi API server to respond to a browser's AJAX request with two things:
- A media file (e.g. an image)
- A JSON object with metadata about the file
These are two separate items only because binary data cannot easily be stored in JSON. Otherwise, this would be a single resource. Nevertheless, it is preferable that they be sent in a single response.
We upload these in a single request with multipart/form-data
. In that case, browsers provide a built-in mechanism to serialize the body and most server-side frameworks know how to parse it. But how does one do the same for a response, in the opposite direction? Namely, how should a server serialize the body to transmit it to a client?
From what I can tell, multipart/mixed
could be a useful content type. But there is very little talk of this. Most people seem to resort to providing two separate GET
routes, one for each piece. I dislike that because it opens you up to race conditions, amongst other things. What am I missing?
See also my question in hapijs/discuss#563.
data URI
withinJSON
response?.
delimiter work for you? You can send it as a string and then decode it on the front.multipart/form-data
and useResponse.formData()
response.formData()
. That will be useful here. Now I have to figure out how to construct the response on the server. I basically need the inverse of pez.