Leiten Sie eine hochgeladene Datei von einem remote server mit dem node (am besten mit gleichen Dateinamen)

Ich möchte eine Datei hochladen, in meinem app.js server , die sollten die Leitung, die Datei auf eine crossdomain-server wie meine upload.js server.

Den vollständigen code finden Sie unter dem folgenden link

Den upload.js server arbeitet. Mein problem ist das app.js server. Anfrage scheint in der Lage sein, streaming-Dateien (https://github.com/request/request#streaming). Aber ich bekomme es nicht funktioniert.
Ich bin auch immer : [Fehler: Ungültiges Protokoll] in meinem app.js.

Es musst diese Zeile:

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

Änderte ich die post zu setzen und in meiner upload.js die post-Methode auch, aber es ergibt den gleichen Fehler.

Mein Ziel ist es, eine Datei hochladen, von der html-Seite auf localhost:3000/upload die Rohre die Datei auf localhost:4000/hochladen (am besten mit gleichen Dateinamen) . Aber ich bekomme es nicht funktioniert (diese post nicht hat mir geholfen).

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');
});

InformationsquelleAutor svenhornberg | 2015-08-21

Schreibe einen Kommentar