【问题标题】:Open a cross domain file - .htaccess limitation or fs limitation? node.js打开跨域文件 - .htaccess 限制或 fs 限制?节点.js
【发布时间】:2014-02-28 23:38:25
【问题描述】:

我被难住了。

所以我正在尝试通过 fs 跨域读取文件。库。它回应说它不存在。我试图用我的浏览器打开文件,它工作得很好。我应该使用不同于 fs 的东西吗?

我尝试添加一个端口http://www.mydomain.com:80/uploads/docs/doc.doc,但这也不起作用。

在 plesk 面板上运行虚拟主机。 (阿帕奇)

【问题讨论】:

  • 你需要显示失败的代码。

标签: javascript node.js .htaccess plesk


【解决方案1】:

我应该使用不同于 fs 的东西吗?

是的。 fs module 用于与机器的本地文件系统交互,而不是处理 network communication 诸如 HTTP 请求和响应。

您可以使用http module,特别是http.get(),从HTTP 服务器请求文件:

http.get('http://www.mydomain.com/uploads/docs/doc.doc', function (res) {
    // process the response, `res`, as desired

    if (res.statusCode === 200) {
        // Example, piping the response to a file on disk:

        res.pipe(fs.createWriteStream(__dirname + '/doc.doc'));
    } else {
        console.error('The address is unavailable. (%d)', res.statusCode);
    }
});

res 将是一个http.IncomingMessage,即stream.Readables

【讨论】:

  • 我知道这将是一个 fs 问题。谢谢。
猜你喜欢
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2018-07-19
  • 1970-01-01
相关资源
最近更新 更多