im new to nodejs and working on a app that can accept file upload and download using express js, the frontend side upload feature just never receive the error response(validation not passed, etc.) sent from the sever side, always stay on pending status in browser dev tools, but the success response can be received and processed.
the same request POST via Postman returned the expected result both error and success.
i use busboy to handle the upload multipart data and wrap it up with Promise hope to wait for the upload progress finished, here is the code
let bb = busboy({headers: req.headers});
bb.on("file", (name, stream, info) => {
try {
let decodedName = decodeURIComponent(info.filename);
Helper.checkNameParamsInvalidity(decodedName);
let p = `${path}/${decodedName}`;
if (fs.existsSync(p)) {
req.unpipe(bb);
req.pause();
reject(p));
return;
}
let writeStream = fs.createWriteStream(p);
stream.pipe(writeStream);
} catch (e) {
reject(e);
}
});
bb.on("close", () => resolve(true));
req.pipe(bb);
and here is the frontend code for Axios API
upload: (path: string, object: FormData, bi: string) =>
instance.post(LEVEL + "/upload", object,
{headers: {__instance__: bi, __path__: path}, timeout: 0})
Axios response interceptor
async (error) => {
console.log(error);
let router = useRouter();
let complex = error.response;
if (complex.data instanceof Blob) {
complex.data = JSON.parse((await (complex.data as Blob).text()));
}
ElNotification({
type: "error",
title: complex.data["code"] || error.name,
message: complex.data["message"] || error.message
});
if (complex.data["code"] === "NOT_AUTHED") {
router.push({name: "auth"});
}
return Promise.reject(complex.data);
}
im not sure and confused with which function should i call to tell the request(client) to end the mulitpart data transmission and prepare to receive the response from the server,
Because seems like it just ignore the response from the server(or unable to receive it due to the data transmission?)
And the Vite(dev tool) proxy server tells me Error: write ECONNABORTED after the server sends a error message,
Also i check the package from the server using Wireshark make sure the error response is sent after the error occurred.
i have tried using different browser from different devices even different networks but no one works.
and using destroy(), unpipe(), pause(), throwing errors, and send() response via request object hope to end the request but still not working and some causing the server app to crash.
i expect the upload POST(or anything else) request can be stop or receives the error message even the file size in the multipart is larger than 1MB.
or a way(library) to terminate the upload progress if the validation cannot passed.
many thanks.