Search code examples
node.jsdownloadfileserver

nodes; user download via remote fileserver


browser request - webserver - fileserver (only access from webserver ip) - webserver - browser download

I have a webserver and a fileserver. The webserver is allowed to access the fileserver (ip filter). I would like to share a file/make it downloadable from the fileserver, but without having to make a public access on the fileserver. I'm thinking about making a api on the fileserver (small webserver)

router.get('/download', async (req, res) => {
    res.header("Access-Control-Allow-Origin", "https://testserver");
    res.header("Access-Control-Allow-Headers", "X-requested-With");
    res.download(__dirname + '/../download/'+req.query.file, req.query.file)
})

And then download the file via the webserver and then send it to the browser/user

router('/downloadOnTheWebserver', (req,res) => {
...
res.download(request('testserver' + someFileToDownload').pipe(make a downloadstream somehow)) 

Is that the way to do this, and how...?


Solution

  • Well I did this:

    Fileserver:

    router.get('/download', async (req, res) => { 
    res.download(__dirname + '/folder/filename')
    

    })

    Webserver:

    var pdfstream = request.get("https://downloadlinkonfileserver)
    res.attachment('filename'); 
    pdfstream.pipe(res)
    res.on('finish',function (err,data){
      if(err){
        throw err
      } else {
        console.log('It went well');
    }