如何使用 request 将通过 Node.js 的大文件请求重定向到不同的服务器

发布于 2025-01-10 00:44:08 字数 1599 浏览 0 评论 0原文

我有一个 node.js 服务器,它将 API 请求转发到不同端口上的另一台服务器(以便身份验证 cookie 等能够通过),这一切都工作得很好,直到客户端需要上传一个大文件( > 100mb)。

当我尝试这样做时,如果文件超过~30mb,则请求甚至永远不会到达远端服务器(当直接连接时,它会很乐意接受大文件),所以我很确定它正在节点中死亡……某处。

"use strict";
const http = require("http");
const port = process.env.FRONTEND_PORT || 13370;
const path = require("path");
const request = require("request");
const express = require("express");

const app = express();

const staticPath = path.join(__dirname, "/dist/");
app.set("port", port);

// Attempt to set some ridiculously high limits on things
app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));

// Serve files from this location
app.use(express.static(staticPath));

app.use(
  '/api',
  (req, res) =>
  {
    var url = "https://localhost:12121/api" + req.url;
    req.pipe(request(url)).pipe(res);
    console.log(res.statusCode);
    console.log(res.statusMessage);
  });


// If we hit any path that doesn't exist, instead serve up index.html and let react router handle it
app.get("*", (req, res) => { res.sendFile(path.join(__dirname, "/dist/index.html")); });

app.listen(app.get("port"), () => { console.log("listening"); });

正如你所看到的,我尝试使用以下方法提高限制(基于我见过的其他答案):

app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));

...但这在这种情况下似乎对我没有帮助。我确信我错过了一些明显的东西,但我对 Node.js 非常陌生(老实说,对于一般的 Web 开发)。

(还值得注意的是,文件是通过基本的 XMLHttpRequest 作为 POST 发送的,通过发送 File 对象 - 没有什么花哨的事情发生)

I have a node.js server that is forwarding API requests through to another server on a different port (so that auth cookies and the like make it across), and this has all worked great until the client has needed to upload a large file (> 100mb).

When I try to do that, if the file is over ~30mb the request never even reaches the far server (which will happily accept large files when connected to directly), so I'm pretty sure it's dying in node ... somewhere.

"use strict";
const http = require("http");
const port = process.env.FRONTEND_PORT || 13370;
const path = require("path");
const request = require("request");
const express = require("express");

const app = express();

const staticPath = path.join(__dirname, "/dist/");
app.set("port", port);

// Attempt to set some ridiculously high limits on things
app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));

// Serve files from this location
app.use(express.static(staticPath));

app.use(
  '/api',
  (req, res) =>
  {
    var url = "https://localhost:12121/api" + req.url;
    req.pipe(request(url)).pipe(res);
    console.log(res.statusCode);
    console.log(res.statusMessage);
  });


// If we hit any path that doesn't exist, instead serve up index.html and let react router handle it
app.get("*", (req, res) => { res.sendFile(path.join(__dirname, "/dist/index.html")); });

app.listen(app.get("port"), () => { console.log("listening"); });

As you can see, I've tried bumping up the limits (based on other SO answers I've seen) using:

app.use(express.urlencoded({parameterLimit: 1000000, limit: '10gb', extended: true}));
app.use(express.json({limit: '10gb'}));

...but this doesn't seem to be helping me in this case. I'm sure I'm missing something obvious, but I am extremely new to node.js (and, honestly, web development in general).

(It's also worth noting, the file is being sent through a basic XMLHttpRequest as POST, sending a File object through - nothing fancy going on there)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文