管道超级代理响应以表达响应 [英] Pipe superagent response to express response

查看:79
本文介绍了管道超级代理响应以表达响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Express应用程序代理"某些文件.为什么下面的代码不起作用?

I'm trying to "proxy" some file with an express app. Why the code below doesn't work?

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

当我直接获取"文件时,它可以工作:

When I "wget" a file directly it works:

$ 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]

当我打电话给我的enpdpoint时,它永远不会结束:

when I call my enpdpoint it never finishes:

$ 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...

一些细节:

$ npm -v
3.9.5

$ npm list --depth=0
express-superagent-pipe-file
├── express@4.14.0
└── superagent@2.1.0

推荐答案

超级代理的响应对象不应视为流,因为它可能已经是自动序列化的结果(例如,从JSON到JavaScript对象). 关于管道数据的文档而不是使用响应对象,指出您可以直接管道超级代理对流的请求:

A superagent's response object should not be treated as a stream, because it may already be the result of automatic serialization (e.g. from JSON to a JavaScript object). Rather than using the response object, the documentation on piping data states that you can directly pipe the superagent request to a stream:

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

这篇关于管道超级代理响应以表达响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆