node.js - Node http.大文件传输失败并显示 'ERR_STREAM_PREMATU

我正在尝试将 10 GB 数据从 Node 客户端发送到 Node http 服务器。它工作正常,直到我达到大约 5 GB。协议(protocol)是http。

它工作正常,直到我达到大约 5 GB。我在服务器端遇到的错误是 [ERR_STREAM_PREMATURE_CLOSE],在客户端是 RequestError: read ECONNRESET。

示例服务器:

const http = require('http')
const fs = require('fs')
const util = require('util')
const stream = require('stream')

const pipeline = util.promisify(stream.pipeline)
const host = 'localhost'
const port = 4000

const server = http.createServer(async (req, res) => {
  try {
    const writeStream = fs.createWriteStream('file', { encoding: 'binary' })
    await pipeline(req, writeStream)
  } catch (e) {
    console.log(e)
  }
})

server.listen(port, host, () => {
  console.log(`Server is running on http://${host}:${port}`)
})

示例客户端

const got = require('got')
async function upload () {
  await pipeline(fs.createReadStream(filename), got.stream.post('http://localhost:4000/upload'))
}

我尝试过不同的服务器(原始 http 和 express)和客户端(原始 Node 、请求、 Node 获取)。我还尝试了与 busboy 的 multipart。同样的问题。

我正在 Mac 上尝试运行 Node v14.4.0。

最佳答案

我尝试在我的计算机上传输一个 10 GB 的文件,它对我有用。

服务器示例:

const http = require('http');
const fs = require('fs');

const PORT = 8333;

const server = http.createServer((req, res) => {
    const writeStream = fs.createWriteStream('./result.txt');
    req.pipe(writeStream);
    req.on('end', () => {
        console.log('The file was successfully written.');
        res.end('OK');
    });
});

server.listen(PORT, () => {
    console.log(`Server has been started on port ${server.address().port}`);
});

客户端示例

const http = require('http');
const fs = require('fs');

function upload() {
    const readStream = fs.createReadStream('./test.txt');
    const request = http.request('http://localhost:8333', { method: 'POST' }, (res) => console.log(`STATUS: ${res.statusCode}`));
    readStream.pipe(request);
    readStream.on('end', () => request.end());
}

upload();

我并不是说这段代码非常正确,但您可以尝试一下,因为它对我有用。

我在 Node.js v12.4.1 和 Windows 10 上运行了这个例子。

关于node.js - Node http.大文件传输失败并显示 'ERR_STREAM_PREMATURE_CLOSE',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62364197/

相关文章:

reactjs - WebSocket SocketIO 连接无法与 Heroku 上的 NestJ

c# - 使用 IIS 时出现多个错误

ios - 如何从 iOS (Swift) 连接到 MongoDB

javascript - react-router-dom 不工作只是渲染 "/"

linux - docker 内部出现 fatal error : unable to access

linux - tar: 目录: 无法 rmdir: 目录不为空

javascript - 为什么我的 JavaScript 在使用 Blazor 时不能正确呈现?

python-3.x - pynput pip3 安装错误 : Could not find a v

python - 如何在 Excel 的 Power Query 中运行 Python 脚本

python - `join` 或 `format` 字符串哪个更好?