文件系统读取文件,确切的https路径如何给定和读取文件
var path_name : https://example.s3.ap-south-1.amazonaws.com/kc/insp_report1.pdf
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return its content:
fs.readFile(path_name, function(err, data) {
res.writeHead(200, {'Content-Type': 'application/pdf'});
res.write(data);
return res.end();
});
}).listen(8080);
我的错误是它也会占用我的系统路径
{ 错误:ENOENT:没有这样的文件或目录,打开 'C:\Users\example\Desktop\react\manyuBackEnd\https:example.s3.ap-south-1.amazonaws.comkcinsp_report1.pdf'
fs
代表文件系统,它用于操作驻留在主机上的文件 - 你不能使用fs
来读取驻留在不同服务器上的文件,除非你可以直接访问它(例如,两个服务器共享同一个网络(。
您需要从服务器发出GET请求,以通过https或第三方库(如axios或request(下载文件
我假设您想从path_name中的链接下载pdf,然后将pdf保存到本地文件。你想像詹姆斯建议的那样发出GET请求来请求数据。您必须创建一个写入流,然后处理来自 get 请求的响应。
var file = fs.createWriteStream('file_path');
https.get('your url', (res) => {
res.on('data', (chunk) => { file.write(chunk); });
res.on('end', () => { file.end() }
});