0

我正在使用外部服务来创建图像。我希望我的用户能够点击我的 API 并请求图像。然后我的 Express 服务器将从外部服务中检索它,然后将其提供给用户。我想有点像代理,但不完全是。

有没有一种简单的方法可以做到这一点,最好是不涉及将图像下载到硬盘驱动器,然后将其读回并提供服务?

使用请求库,我能够想出这个:

var request = require("request");

exports.relayImage = function(req, res){
    request(req.params.url).pipe(res);
}

这似乎行得通。如果有更有效的方法来做到这一点(意思是服务器资源,而不是代码行),说出来!

4

2 回答 2

2

What you are doing is exactly what you should be doing, and is the most efficient method. Using pipe, the data is sent as it comes in, requiring no additional resources than are needed to buffer and transmit.

Also be mindful of content type and other response headers that you may want to relay. Finally, realize that you've effectively built an open proxy where anyone can request anything they want through your servers. This is a bit dangerous, so be sure to lock it down in your final application.

于 2013-08-03T02:35:26.807 回答
0

您应该能够使用http模块向外部图像服务发出请求,并通过回调将图像作为响应返回。除非您明确告诉它,否则它不会写入磁盘。

于 2013-08-02T21:35:15.037 回答