我遇到一种情况,我正在从 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;
}
}));
因此您可以将您选择的请求重定向到另一台服务器。
关于javascript - 将文件从express js传输到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197352/