Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. How can I download a file that is in my server to my machine accessing a page in a nodeJS server? Express has a helper for this to make life easier. As far as your browser is concerned, the file's name is just 'download', so you need to give it more info by using another HTTP header.
You can set the header value to whatever you like. In this case, I am using a mime-type library - node-mime , to check what the mime-type of the file is. Another important thing to note here is that I have changed your code to use a readStream. This is a much better way to do things because using any method with 'Sync' in the name is frowned upon because node is meant to be asynchronous.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
What would I place as the cb argument? Abdul Sounds like you are very new to node. Take a look at this tutorial: tutorialspoint. Abdul maybe it would be good if you share with the rest of the class what you have figured out? Is there a way to see the speed of the download? Show 7 more comments. Kelvin Omereshone 2 2 silver badges 11 11 bronze badges. Abdul You specify the callback with a function only if you need to do something when the file has been successfully fetched.
It would be good to check the status code before saving: response. But we don't check the result return cb err.
Buzut Buzut 4, 4 4 gold badges 41 41 silver badges 50 50 bronze badges. The request module just works straight for HTTPs.
S Ventura. It's more error prone without a doubt. Anyway, in any case where using request module is an option, I'd advise it as it's way higher level and thus, easier and efficient. Alex, nope, this is an error message and there's a return.
So if response. Thank you for showing example using request module. Show 3 more comments. MikeL 5, 38 38 silver badges 41 41 bronze badges. Augusto Roman Augusto Roman 6 6 silver badges 4 4 bronze badges. What for are you storing request into a variable? You are right, there is no need to save the request, its not used anyway. That's what you mean? Yes, I think that's what polkovnikov. Per default the fs. Bjarke Pjedsted Bjarke Pjedsted 4 4 silver badges 8 8 bronze badges.
Two comments on this: 1 it should probably reject Error objects, not strings, 2 fs. This works great! No errors popped up but the. Removing the logic for the flag fixed it. Just wanted to point that out if someone had the issues with the approach. Is there a memory leak in this answer: stackoverflow. Leave a Reply Cancel reply Your email address will not be published. Follow us Facebook Youtube Github. Tools Json Formatter.
We use cookies to improve your experience with the site. Close Privacy Overview This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website.
The only difference is that the data and end events are listened on the stdout object of spawn. Also we listen to spawn 's exit event to make note of any errors. Although it says downloading using wget , this example applies to downloading using curl with the -O option too.
This method of downloading looks the most simple from coding point of view. Why exec and not spawn? Because we just want wget to tell us if the work was done properly or not, we are not interested in buffers and streams. We are making wget do all the dirty work of making request, handling data, and saving the file for us.
Built-in modules Node. You might also like How to terminate a Node.
0コメント