-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify comments + add field handler to multipart sample #609
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,20 +148,31 @@ exports.uploadFile = (req, res) => { | |
if (req.method === 'POST') { | ||
const busboy = new Busboy({ headers: req.headers }); | ||
|
||
// This object will accumulate all the fields, keyed by their name | ||
const fields = {}; | ||
|
||
// This object will accumulate all the uploaded files, keyed by their name. | ||
const uploads = {}; | ||
const tmpdir = os.tmpdir(); | ||
|
||
// This callback will be invoked for each file uploaded. | ||
// This event will be triggered for each non-file field in the form. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit afraid that users may confuse events described above, with event that trigger a function. For instance, users may think that there are many function executions here. I think the previous form ("This callback will be invoked") was a bit better Also, busboy docs say that events are emitted, not triggered. How about something like this:
|
||
busboy.on('field', (fieldname, val) => { | ||
// TODO(developer): Process submitted field values here | ||
console.log(`Processed field ${fieldname}: ${val}.`); | ||
fields[fieldname] = val; | ||
}); | ||
|
||
// This event will be triggered for each file uploaded. | ||
busboy.on('file', (fieldname, file, filename) => { | ||
// Note: os.tmpdir() points to an in-memory file system on GCF | ||
// Thus, any files in it must fit in the instance's memory. | ||
console.log(`Processed file ${filename}`); | ||
const filepath = path.join(tmpdir, filename); | ||
uploads[fieldname] = filepath; | ||
file.pipe(fs.createWriteStream(filepath)); | ||
}); | ||
|
||
// This callback will be invoked after all uploaded files are saved. | ||
// This event will be triggered after all uploaded files are saved. | ||
busboy.on('finish', () => { | ||
// TODO(developer): Process uploaded files here | ||
for (const name in uploads) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add an empty line before:
const tmpdir = os.tmpdir();
Otherwise it looks like the comment "This object will accumulate all the uploaded files, keyed by their name" is for the two lines.
You can group this "const tmpdir" together with "const busboy"