javascript - 将文件从express js传输到另一台服务器

标签 javascript jquery node.js express multipartform-data

我遇到一种情况,我正在从 Jquery 向我的 Express 服务器发送文件,并且我需要将该请求传输到另一台服务器,而不解析 Express 服务器中的文件。这是我到目前为止使用过的代码片段 jQuery

$.ajax({
            url: 'api/auth/handle',
            type: 'POST',
            data: data, // formData
            cache: false,
            dataType: 'json',
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success: function (data, textStatus, jqXHR) {
                console.log("success");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // Handle errors here
                console.log('ERRORS: ' +textStatus);
                // STOP LOADING SPINNER
            }
        });

Express js代码

module.exports.handleFile = (req, res) => {
    console.log(req);
    let data = request.post("http://localhost:5002/files/pdf", function (err, resp, body) {
        //console.log(err);
        if (err) {
            console.log('Error!');
        } else {
            console.log('URL: ' + body);
        }
    });
    let form = data.form();
    form.append('file', '<FILE_DATA>', req);
    res.status(200).send({ "success": "success" });

};

问题是我没有在第二台服务器上获取表单数据。 任何建议都会有帮助。谢谢

最佳答案

我建议使用 Node.js/Express 的代理模块。其中一个可用的称为 express-http-proxy

其文档中的示例用法:

var proxy = require('express-http-proxy');

var app = require('express')();

app.use('/proxy', proxy('www.google.com', {
  forwardPath: function(req, res) {
    return require('url').parse(req.url).path;
  }
}));

因此您可以将您选择的请求重定向到另一台服务器。

参见https://github.com/villadora/express-http-proxy了解更多信息!

关于javascript - 将文件从express js传输到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197352/

相关文章:

node.js - 带有 Node 服务器和react-admin的API URL

javascript - 如何正确检测 Node.js 中 JSON 对象中的转义字符?

javascript - 为什么脚本在 body 标签的末尾

javascript - 在 push 开发时防止 BitBucket 部署到 heroku

javascript - 编辑 Stripe 嵌入表单

jquery - ruby on rails 中的 Bootstrap 模态不工作(不显示)

node.js - 在 Node.js 中使用 fast-csv 包读取和写入 CSV

javascript - 如何在 Controller 中格式化 JSON 数据

javascript - axios 多然后的

javascript - 按数字处理输入字段