将文件从express js传输到另一台服务器
问题描述:
我有一种情况,即将文件从Jquery发送到我的express服务器,并且我需要将该请求传输到另一台服务器而不解析express服务器中的文件。这里是代码片段我已经使用到现在 jQuery的将文件从express js传输到另一台服务器
$.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
}
});
快递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;
}
}));
所以,你可以重定向你的选择到另一台服务器的请求。
有没有其他方式没有设置代理?可能以某种方式使用请求模块? –
Sry我不知道任何聪明的,短的解决方案达到相同的。代理方式就是我要去的方式,但也许有人有不同的解决方案。 –
我会给代理一个确定的尝试。谢谢 –