gpt4 book ai didi

node.js - 管道 super 代理响应以表达响应

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:00 26 4
gpt4 key购买 nike

我正在尝试使用 Express 应用程序“代理”一些文件。为什么下面的代码不起作用?

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

当我直接“wget”一个文件时,它起作用了:

$ 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 时它永远不会完成:

$ 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

最佳答案

super 代理的响应对象不应被视为流,因为它可能已经是自动序列化的结果(例如,从 JSON 到 JavaScript 对象)。 documentation on piping data 不是使用响应对象,而是声明您可以将 super 代理请求直接通过管道传输到流:

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

关于node.js - 管道 super 代理响应以表达响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50888838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com