2016-07-20 11 views
1

Ich versuche, einige Datei mit einer Express-App "Proxy". Warum funktioniert der folgende Code nicht?Rohr superagenten Antwort auf Express-Antwort

var app = require('express')() 
var request = require('superagent') 
app.get('/image', function(req, res, next) { 
    request('http://s3.amazonaws.com/thumbnails.illustrationsource.com/huge.104.520060.JPG') 
    .then(function(_res) { 
     _res.pipe(res) 
    }) 
}) 

app.listen(3001, function() { 
    console.log('listen') 
}) 

Als ich "wget" eine Datei direkt funktioniert es:

$ wget http://s3.amazonaws.com/thumbnails.illustrationsource.com/huge.104.520060.JPG 
--2016-07-20 11:44:33-- http://s3.amazonaws.com/thumbnails.illustrationsource.com/huge.104.520060.JPG 
Resolving s3.amazonaws.com... 54.231.120.106 
Connecting to s3.amazonaws.com|54.231.120.106|:80... connected. 
HTTP request sent, awaiting response... 200 OK 
Length: 21026 (21K) [image/jpeg] 
Saving to: 'huge.104.520060.JPG' 

huge.104.520060.JPG       100%[==============================================================================================>] 20.53K --.-KB/s in 0.1s 

2016-07-20 11:44:34 (203 KB/s) - 'huge.104.520060.JPG' saved [21026/21026] 

, wenn ich meine enpdpoint nennen es nie endet:

$ wget localhost:3001/image 
--2016-07-20 11:45:00-- http://localhost:3001/image 
Resolving localhost... 127.0.0.1, ::1 
Connecting to localhost|127.0.0.1|:3001... connected. 
HTTP request sent, awaiting response... 

Einige Details:

$ npm -v 
3.9.5 

$ npm list --depth=0 
express-superagent-pipe-file 
├── [email protected] 
└── [email protected] 

Antwort

2

Das Antwortobjekt einer Superaktivität sollte nicht tre sein als ein Strom, weil es möglicherweise bereits das Ergebnis der automatischen Serialisierung (z. von JSON zu einem JavaScript-Objekt). Anstatt das Response-Objekt verwenden, die documentation on piping data Zustände, die Sie direkt Rohr der Superagent-Anforderung an einen Bach:

var app = require('express')() 
var request = require('superagent') 
app.get('/image', function(req, res, next) { 
    request('http://s3.amazonaws.com/thumbnails.illustrationsource.com/huge.104.520060.JPG') 
    .pipe(res) 
}) 

app.listen(3001, function() { 
    console.log('listen') 
}) 
+0

Dank! Es klappt. Aber was macht '_res.pipe' in diesem Fall? – kharandziuk

+1

@kharandziuk Es scheint implementationsabhängig zu sein. Ein 'Response'-Objekt ist in der Tat ein' ReadableStream' in Node.js, nach [dem Quellcode] (https://github.com/visionmedia/superagent/blob/v2.1.0/lib/node/response.js# L26-L27), aber es ist eine private API, die nicht extern verwendet werden sollte. Nichts in der Dokumentation zeigt, dass ein Antwortobjekt trotzdem als lesbarer Stream verwendet werden kann. –

+1

Falls Sie sich noch fragen, was nach '_res.pipe' passiert, hier ein guter Kommentar: https://github.com/visionmedia/superagent/issues/684#issuecomment-233959045 –

0

mit dem Versprechen, die folgende Art und Weise herunterladen:

const fs = require('fs'); 
const path = require('path'); 

const download = (url) => { 
    return superagent.get(url) 
    .then((response) => { 
     const stream = fs.createWriteStream('file.ext'); 
     return response.pipe(stream); 
    }); 
};