【问题标题】:Send other domain's file from expressJs从 expressJs 发送其他域的文件
【发布时间】:2019-01-29 10:51:23
【问题描述】:

我有一个服务器(nodeJS、expressJS)。 我创建了一个路由 /abc,我想在其中发送一个存储在 Google Cloud Bucket 中的 HTML 文件。

我尝试使用 res.sendFile 但由于它从内部服务器发送文件,所以它对我不起作用。

我的 res.sendFile 代码:-

router.get('/abc', function(req, res) {
res.sendFile("https://storage.googleapis.com/BUCKET_NAME/FILENAME.html");
});

如何实现这种场景。 提前致谢。

【问题讨论】:

  • Google Cloud Bucket 文件是否已打开或需要登录?
  • 我暂时将文件设为公开。而且是普通的html文件
  • 检查Hosting a Static Website可能对你有用

标签: node.js express sendfile


【解决方案1】:

使用请求 npm :https://www.npmjs.com/package/request

app.get('/abc', function(req, res) {
 req.pipe(request('https://storage.googleapis.com/BUCKET_NAME/FILENAME.html')).pipe(res)
})

【讨论】:

    【解决方案2】:

    类似的东西

    import http;
    var pipeRequest=(url,res)=>
        http.get(url, (getRes)=>{
          res.setHeader("content-type", getRes.headers['content-type']);
          getRes.pipe(res);
          }
        );
    
    router.get('/abc', function(req, res) {
       pipeRequest("https://storage.googleapis.com/BUCKET_NAME/FILENAME.html",res);
    });
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2011-07-08
      • 2011-07-24
      相关资源
      最近更新 更多