将上传的文件传输到具有节点的远程服务器(最好使用相同的文件名)

我想上传一个文件在我的app.js服务器,应该将该文件传输到像我的upload.js服务器的跨域服务器。

完整的代码可以在下面的链接中find

upload.js服务器正在工作。 我的问题是app.js服务器。 请求似乎能够stream式传输文件( https://github.com/request/request#streaming )。 但我没有得到它的工作。 我总是得到:[错误:无效的协议]在我的app.js。

这是这条线:

fs.createReadStream(file.path).pipe(request.post('localhost:4000/upload')) 

我改了post,并在我的upload.js post方法也放,但它导致相同的错误。

我的目标是从HTML页面上传一个文件到本地主机:3000 /上传pipe道文件到本地主机:4000 /上传(理想的是相同的文件名)。 但我没有得到它的工作(这篇文章没有帮助我)。

app.js:

 var express = require('express') , multiparty = require('multiparty') , request = require('request') , fs = require('fs') , util = require('util') , http = require('http'); var app = express(); app.use('/static', express.static('static')); process.on('uncaughtException', function (err) { console.log(err); }); app.get('/', function (req, res) { res.redirect('static/index.html'); }); app.post('/upload', function(req, res, next){ //https://github.com/request/request#streaming var form = new multiparty.Form(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(util.inspect({fields: fields, files: files})); }); form.on('file', function(name,file) { //stream it to localhost:4000 with same name fs.createReadStream(file.path).pipe(request.post('localhost:4000/upload')) console.log(file.path); }); }); var server = app.listen(3000, '0.0.0.0' ,function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); }); 

index.html的:

 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Upload</title> </head> <body> <h2>Upload</h2> <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" id="file" name="file" /> <button>upload</button> </form> </body> </html> 

upload.js:

 var express = require('express') , multiparty = require('multiparty') , cors = require('cors') , util = require('util') , app = express(); app.use(cors()); process.on('uncaughtException', function (err) { console.log(err); }); app.get('/', cors(), function(req, res, next){ res.json({msg: 'This is CORS-enabled for all origins!'}); }); app.post('/upload', cors(), function(req, res, next){ var form = new multiparty.Form(); form.parse(req, function(err, fields, files) { res.writeHead(200, {'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(util.inspect({fields: fields, files: files})); }); form.on('file', function(name,file) { console.log(file); console.log(name); }); }); app.listen(4000, function(){ console.log('CORS-enabled web server listening on port 4000'); }); 

更新

我相信你错过了url的协议。 它应该工作,如果您将http协议添加到url:

 fs.createReadStream(file.path).pipe(request.post('http://localhost:4000/upload')) 

使上传工作

当您将文件内容传输到upload.js中的POST函数时,多部分表单数据将丢失。 您需要创build一个新的POST请求并传递原始文件内容。

app.js执行以下操作:

  form.on('file', function(name, file) { var formData = { file: { value: fs.createReadStream(file.path), options: { filename: file.originalFilename } } }; // Post the file to the upload server request.post({url: 'http://localhost:4000/upload', formData: formData}); } 

这也将传递原始文件名。 有关更多信息,请参阅: https : //github.com/request/request#multipartform-data-multipart-form-uploads

我有一个类似的问题,但我想直接stream文件到远程服务器,而不是先保存在本地。 这是我的修改,让stream媒体工作:

 app.post('/upload', function(request, response, next) { var form = new multiparty.Form(); form.on('part', function(formPart) { var contentType = formPart.headers['content-type']; var formData = { file: { value: formPart, options: { filename: formPart.filename, contentType: contentType, knownLength: formPart.byteCount } } }; requestJs.post({ url: 'http://localhost:4000/upload', formData: formData, // These may or may not be necessary for your server: preambleCRLF: true, postambleCRLF: true }); }); form.on('error', function(error) { next(error); }); form.on('close', function() { response.send('received upload'); }); form.parse(request); });