Search code examples
javascriptnode.jsexpresspathstatic-files

How to serve static file and give its path to one user?


I am using jsmodeler (https://github.com/kovacsv/JSModeler) to show 3d models on my site. The only options for the user to select a file are through filepicker and through putting the path to the file in the url (ie http://localhost:3000/ModelView#https://cdn.rawgit.com/kovacsv/Online3DViewer/9e9ca71d/website/testfiles/cube.3ds). I would like to send a file through my node js server and then load the file into the viewer.

I think I need to put the path to the file that I am serving into the url and refresh the page? But how do I get this path? It should only be available to one user so I cannot put it in the public folder!

Say I send it with

res.sendFile(myFile)

This does not send the path to the file right? I'm also worried that even if I get the path, when I refresh the page at the new url, the file will not be there anymore.

UPDATE: So I am thinking when the model is saved on my server, I will save it in the public folder, but under a random number, ie "path/1982746/model.obj". That way no one can just type the model into the url. Is this safe, or can someone easily see all the files that are public?


Solution

  • Yes the random number would help obfuscate the file from other users. You can upload the file to a node server using 'multipart/form-data' in the post request and save it in a location. Once saved, you can do something like

    res.redirect('http://example.com/mypage/'+generatedNumber);
    

    On the redirected page, you can form the appropriate path and load the file as it was any other file in your server directory.