Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 请求超时:检索文件时获取请求超时_Node.js_Http - Fatal编程技术网

Node.js 请求超时:检索文件时获取请求超时

Node.js 请求超时:检索文件时获取请求超时,node.js,http,Node.js,Http,我需要在我的应用程序中显示上载到其他服务器的PDF文件。我使用了一个iframe标记,src属性指向所需的URL 我还想允许用户打印上述PDF文件,但我不能,因为 因此,我决定从我的服务器向另一台拥有PDF文件的服务器执行GET请求——我的想法是将PDF从另一台服务器获取到我的服务器中,然后将iframe标记的src属性替换为我的服务器处理并用PDF文件响应的路由,如果这有道理的话 我的GET请求如下所示: var options = { host: host, //the hostna

我需要在我的应用程序中显示上载到其他服务器的PDF文件。我使用了一个
iframe
标记,src属性指向所需的URL

我还想允许用户打印上述PDF文件,但我不能,因为

因此,我决定从我的服务器向另一台拥有PDF文件的服务器执行GET请求——我的想法是将PDF从另一台服务器获取到我的服务器中,然后将
iframe
标记的
src
属性替换为我的服务器处理并用PDF文件响应的路由,如果这有道理的话

我的GET请求如下所示:

var options = {
    host: host, //the hostname of the server that contains the PDF
    path: path, //the path on the other server that responds with the PDF
    method: 'GET',
    headers: {}
};

http.request(options, function (response) {
    var body = '';
    response.setEncoding('utf8');
    response.on('data', function (chunk) {
        console.log("Gettin data");
        body += chunk;
    });
    response.on('end', function () {
        var data = null;
        if (body) {
            try {
                data = JSON.parse(body);
            }
            catch (err) {
                data = body;
            }
        }

        res.sendfile(data);
    });
});

问题是请求只是超时。我应该设置一些标题吗?当我直接将URL放在浏览器的地址栏中时,URL工作正常-即显示PDF文件…

根据可以找到的文档,我需要结束请求

因此,添加以下内容解决了问题:

var request = http.request(...);

request.end();

这结束了请求,我不再得到超时时间。

解决了这个问题。调用http请求后,我没有调用
request.end()。