[英]http.get - Wait until file has been generated
我正在使用Web服务从URL生成PDF。 然后,我想将生成的文件保存到文件系统。 问题是,保存的文档为空。 这可能是因为http.get
不会等到生成文件http.get
操作。 如何等待生成的文件?
var url, writeStream;
writeStream = fs.createWriteStream(path.join(dirPath, type + ".pdf"));
url = "http://html2pdf.it?url=" + (encodeURIComponent('http://www.google.com/'));
http.get(url, function(res) {
res.pipe(writeStream);
writeStream.on('finish', function() {
console.log('Path:', path.join(dirPath, type + ".pdf"));
});
});
这是因为您的Web服务向您返回了301
返回码(将您重定向到另一个页面),而节点的http.get()
只是在此处停止。
您应该使用更高级的包装器(例如请求) ,该包装器更易于使用,并且实际上可以完成您真正想要的事情。
请注意,您可以通过console.logging请求中的res
来发现自己:
http.get(url, function(res) {
console.log(res);
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.