创建 ReadStream 并通过 multer 将文件上传到远程服务器

tor*_*ili 5 node.js express multer

所以我想上传大文件到远程,所以我正在创建 ReadStream

const stream = fs.createReadStream(req.file.path);

async uploadStream(stream: NodeJS.ReadableStream, filePath: string) {
        const dir = path.dirname(filePath);
        if (!(await this.exists(dir))) {
            await this.mkdir(dir);
        }

        await this.putStream(stream, filePath);
        return this.url(filePath);
    }



async putStream(stream: NodeJS.ReadableStream, filePath: string) {
        await this.connect();
        const normFIlePath = this.normalizePath(filePath);
        await this.sftp.put(stream, normFIlePath).catch(e => {
            console.log(e);
        });
        await this.disconnect()
    }
Run Code Online (Sandbox Code Playgroud)

上传后,我将数据保存到数据库并返回文件 url

file = await fileRepo.save(file);
res.send({
            id: file.data.itemId,
            src: file.url
        });
Run Code Online (Sandbox Code Playgroud)

文件正在上传,但之后返回错误:

(node:18) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Run Code Online (Sandbox Code Playgroud)