Closed budarin closed 2 years ago
Solved! I must use Promise to wait for busboy as
import Busboy from "busboy";
export const uploadFile = async (ctx, next) => {
const p = new Promise((resolve, reject) => {
const busboy = new Busboy({
headers: { "content-type": ctx.get("content-type") },
});
// busboy initialization
busboy.on('...', (...) => {...});
busboy.on("finish", () => {
ctx.set("Connection", "close");
ctx.status = 200;
ctx.body = "Ok";
});
ctx.req.pipe(busboy);
});
await p.then(() => {
ctx.set("Connection", "close");
ctx.status = 200;
ctx.body = { result: "Ok" };
})
.catch((error) => {
ctx.status = 500;
ctx.body = error.message;
});
}
node.js version: 16.9.0
yarn version: 1.22.15
@koa/router
version: 10.1.1koa
version: 2.13.4Code sample:
Here is a simple uploadFile code
Expected Behavior:
Expected client have received "Ok" text with 200 status
Actual Behavior:
Have 404 error while busboy did its job - save files in stream and fired
finish
event properlyAdditional steps, HTTP request details, or to reproduce the behavior or a test case:
Here is the demo project