gpt4 book ai didi

node.js - 在 POST 请求中转发上传的文件 - Express

转载 作者:太空宇宙 更新时间:2023-11-03 22:27:42 25 4
gpt4 key购买 nike

我希望我的 Web 服务器发布一个文件,然后通过 POST 请求将其转发到另一台服务器。

const pug = require('pug');
var cloudinary = require('cloudinary');
var express = require('express');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var request = require('request');
var bodyParser = require('body-parser');
var JSON = require('JSON');
var https = require('https');
var fs = require('fs');
var request = require('request');
var upload = multer({ dest: 'uploads/' });
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // We are OK with self-signed

var serverURL = 'https://ec2-54-202-139-197.us-west-2.compute.amazonaws.com:443';

var app = express();
var parser = bodyParser.raw();
var result = null;
app.use(parser);

app.set('view engine', 'pug');

app.get('/',upload.single('avatar'), function (req, res) {
return res.render('index.pug', {results: [], result_pics: {}});
});

app.post('/makeRequest*', upload.single('uploadPicture'), function(req, res) {
console.log("Files:");
console.log(req.file);
console.log(req.body);

var r = request.post({url: serverURL, headers: {'User-Agent': 'Client Webserver'}}, function (err, httpResponse, body) {
console.log('got response');
console.log(err);
console.log(body);
body = JSON.parse(body);
content = body['content'];
console.log(content);
message_type = body.message_type;
result_pics = [];
result = [];
if (message_type == "error") {
result = ["ERROR: " + body.content];
}
else if (message_type == "student created") {
result = ["Student created."];
}
else if (message_type == "student deleted") {
result = ["Student deleted."]
}
else if (message_type == "picture inserted") {
result = ["Picture inserted."];
}
else if (message_type == "viewed pictures") {
pics = body.pictures;
for (key in pics) {
result_pics.push(cloudinary.image(pics[key], {width: 300, height: 200, crop: "crop"}));
}
}
else if (message_type == "listing information") {
student_table = body.content["student_table"];
student_to_pic_table = body.content["student_to_pic_table"];

for (i in student_table) {
student = student_table[i];
result.push("Student ID: " + student["student_id"] + " Nickname: " + student["nickname"]);
}
for (i in student_to_pic_table) {
student_pic = student_to_pic_table[i];
result.push("Student ID: " + student_pic["student_id"] + " Pic ID: " + student_pic["image_id"]);
}
}
else {
result = ["ERROR: returned message is not of valid type"];
}

res.render('index.pug', {results: result, result_pics: result_pics});
res.end();
});
var form = r.form();
if (req.file) {
form.append('uploadPicture', req.file);
}
form.append('content', JSON.stringify(req.body));


});

app.listen(3000, function() { console.log('listening'); });

因此,当 req.file 为 true 时,我收到以下错误:

TypeError: source.on is not a function
at Function.DelayedStream.create (/home/daniel/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at FormData.CombinedStream.append (/home/daniel/node_modules/combined-stream/lib/combined_stream.js:43:37)
at FormData.append (/home/daniel/node_modules/form-data/lib/form_data.js:68:3)
at /home/daniel/WebstormProjects/CloudA3 Client/CloudA3/skrpt.js:95:18
at Layer.handle [as handle_request] (/home/daniel/node_modules/express/lib/router/layer.js:95:5)
at next (/home/daniel/node_modules/express/lib/router/route.js:131:13)
at Immediate.<anonymous> (/home/daniel/node_modules/multer/lib/make-middleware.js:53:37)
at Immediate.immediate._onImmediate (timers.js:440:18)
at processImmediate [as _immediateCallback] (timers.js:383:17)
got response
{ [Error: socket hang up] code: 'ECONNRESET' }

我猜测我需要将 req.file 转换为其他类型才能传输它,但我在任何地方都找不到有关此内容的任何信息,也找不到任何示例,也无法找出 req.file 的类型甚至是!

如何解决这个问题?

最佳答案

由于您使用的是 multerDiskStoragereq.file object具有文件内容本地存储在 req.file.path 属性中的完整路径。您可以使用它通过 fs.createReadStream() 创建一个 stream.Readable 并将其直接传递到表单。所以你可以很容易地做这样的事情。

form.append('uploadPicture', fs.createReadStream(req.file.path))

关于node.js - 在 POST 请求中转发上传的文件 - Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43459320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com