-1

file system read file, exact https path how to given and read file

 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);

my error is it will taken my system path also

{ Error: ENOENT: no such file or directory, open 'C:\Users\example\Desktop\react\manyuBackEnd\https:example.s3.ap-south-1.amazonaws.comkcinsp_report1.pdf'

Anil
  • 138
  • 1
  • 13

2 Answers2

0

fs stands for File System, it's used to manipulate files that reside on the host - you can't use fs to read a file that resides on a different server unless you have direct access to it (e.g. both servers share the same network).

You would need to make a GET request from your server to download the file via https, or a third party library like axios or request

James
  • 80,725
  • 18
  • 167
  • 237
0

I assume you want to download the pdf from the link you have in the path_name and then save the pdf to a local file. You want to make the GET request like James suggest to request the data. You have to create a write stream then handle the response from the get request.

var file = fs.createWriteStream('file_path');
https.get('your url', (res) => {
   res.on('data', (chunk) => { file.write(chunk); });
   res.on('end', () => { file.end() }
});
GisliBG
  • 21
  • 1
  • 2