const router = require("koa-router")();
const fs = require("fs");
router.post("/upload", async (ctx){
const file = ctx.request.body.files.file; //
const reader = fs.createReadStream(file.path); //
const ext = file.name.split(".").pop(); //
const upStream = fs.createWriteStream(`upload/${Math.random().toString()}.${ext}`); //
reader.pipe(upStream); //
return ctx.body = "";
})
the above code is uploading files, but this paragraph is not understood:
reader.pipe(upStream); //
return ctx.body = ""; //
my doubt is: stream
takes some time to finish execution, and it is asynchronous. It can be said that here return "upload successfully"
, We all know , return
is equivalent to ending this http
request, so stream