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]
Dank! Es klappt. Aber was macht '_res.pipe' in diesem Fall? – kharandziuk
@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. –
Falls Sie sich noch fragen, was nach '_res.pipe' passiert, hier ein guter Kommentar: https://github.com/visionmedia/superagent/issues/684#issuecomment-233959045 –