您可能希望使用 http.request 以使用重复的标头进行良好的代理和资源加载。
这是 express 中的示例,它将侦听端口 8080,并将使用您从 /skin/* 路由请求的实际 url 向特定服务器发出请求:
var http = require('http'),
express = require('express'),
app = express();
app.get('/skin/*', function(req, res, next) {
var request = http.request({
hostname: 's3.amazonaws.com',
port: 80,
path: '/' + req.params[0],
method: req.method
}, function(response) {
if (response.statusCode == 200) {
res.writeHead(response.statusCode, response.headers);
response.pipe(res);
} else {
res.writeHead(response.statusCode);
res.end();
}
});
request.on('error', function(e) {
console.log('something went wrong');
console.log(e);
})
request.end();
});
app.listen(8080);
为了测试它,在你的机器上运行它,然后转到:http://localhost:8080/skin/nyc1940/qn01_GEO.png
它将从亚马逊加载该图像代理,并返回其标题。您也可以自定义标头,以防止从 S3 发送 XML(当文件不存在时)。
您不需要设置任何标题,因为它们是从 s3.amazon 代理的,它确实为您可靠地设置了正确的标题。
也不access-control-allow-origin
是只有在 AJAX 请求来自另一个域名的资源时才需要它。response.headers
但无论如何,请在发送前随意修改。它是一个简单的对象(console.log 它用于测试)。