Hi everyone
I’m using Nuxt 3.13.
Site: https://genuine-jelly-3eb6da.netlify.app/
DNS issues? No
Build problems? No
I have a form on my site, which make a request to Nuxt server API endpoint (https://nuxt.com/docs/guide/directory-structure/server). I tried to use 2 modules for parsing incoming HTML form data: Formidable and Busboy (via article). Both work great on my machine, but don’t work on Netlify environment. The result on Netlify was the same. Server API always returns:
error decoding lambda response: invalid status code returned from lambda: 0
Debugging on Netlify is not so easy, so I had to use some console.log() functions.
Busboy
import busboy from "busboy";
import path from "path";
import os from "os";
export default defineEventHandler(async (event): Promise<SubmitFormResult> => {
let fields;
let files;
console.log("parse multipart data start");
[fields, files] = await parseMultipartForm(event.node.req);
console.log("parse multipart data end");
return {};
});
function parseMultipartForm(req) {
return new Promise((resolve) => {
// we'll store all form fields inside of this
const fields = {};
const files = {};
console.log("instantiate our busboy instance!");
// let's instantiate our busboy instance!
const bb = busboy({
// it uses request headers
// to extract the form boundary value (the ----WebKitFormBoundary thing)
headers: req.headers,
});
// before parsing anything, we need to set up some handlers.
// whenever busboy comes across a file ...
bb.on("file", (name, file, info) => {
const { filename, encoding, mimeType } = info;
//save to tmp dir
const saveTo = path.join(os.tmpdir(), `busboy-upload-${random()}`);
file.pipe(fs.createWriteStream(saveTo));
files[name] = {
originalFilename: Buffer.from(filename, "latin1").toString("utf8"),
encoding: encoding,
mimetype: mimeType,
filepath: saveTo,
};
});
// whenever busboy comes across a normal field ...
bb.on("field", (fieldName, value) => {
// ... we write its value into `fields`.
fields[fieldName] = value;
});
// once busboy is finished, we resolve the promise with the resulted fields.
bb.on("finish", () => {
console.log("finish processing request");
resolve([fields, files]);
});
console.log("start processing our request");
// now that all handlers are set up, we can finally start processing our request!
req.pipe(bb);
console.log("end processing our request");
});
}
Netlify functions log:
Sep 4, 06:14:11 PM: INIT_START Runtime Version: nodejs:18.v33 Runtime Version ARN: arn:aws:lambda:us-east-2::runtime:b7500cbab7a13e2ee74c533d5f8cf7ca697047a4f35fad455d19cc4fa6ff8b84
Sep 4, 06:14:12 PM: 27bb4594 INFO parse multipart data start
Sep 4, 06:14:12 PM: 27bb4594 INFO instantiate our busboy instance!
Sep 4, 06:14:12 PM: 27bb4594 INFO start processing our request
Sep 4, 06:14:12 PM: 27bb4594 INFO end processing our request
Sep 4, 06:14:13 PM: 27bb4594 Duration: 230.02 ms Memory Usage: 143 MB Init Duration: 1333.49 ms
For some reason, Busboy never finishes processing request (there is no “finish processing request” console message).
Formidable
import formidable from "formidable";
export default defineEventHandler(async (event): Promise<SubmitFormResult> => {
let fields;
let files;
//parse form data via formidable
const form = formidable({ allowEmptyFiles: true, minFileSize: 0 }); //TODO: check empty file on client via yup
try {
console.log("parse multipart data start");
[fields, files] = await form.parse(event.node.req);
console.log("parse multipart data end");
} catch (err) {
console.error(err);
return { isError: true, errorMsg: "Error parsing form data" };
}
return {};
});
Netlify functions log:
Sep 4, 06:41:54 PM: 255b7634 Duration: 6.54 ms Memory Usage: 200 MB
Sep 4, 06:42:01 PM: c649de3e INFO parse multipart data start
Sep 4, 06:42:01 PM: c649de3e Duration: 59.69 ms Memory Usage: 201 MB
For some reason, Formidable never finishes processing request (there is no “parse multipart data end” console message).
I’ve spend whole day for that problem and I really need some help.
I found 2 similar problems on forum, but there is no answer: