标签云

微信群

扫码加入我们

WeChat QR Code

How do I download a file with Node.js without using third-party libraries?I don't need anything special. I only want to download a file from a given URL, and then save it to a given directory.


"download a file with node.js" - do you mean upload to the server? or retrieve a file from a remote server using your server? or serve a file to a client for download from your node.js server?

2019年05月24日50分35秒

"I only want to download a file from a given url, and then save it to a given directory," it seems pretty clear. :)

2019年05月24日50分35秒

Joseph is making an incorrect assertion that all node processes are server processes

2019年05月24日50分35秒

I got the following console output when I ran this script: node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: connect ECONNREFUSED at errnoException (net.js:646:11) at Object.afterConnect [as oncomplete] (net.js:637:18) .

2019年05月24日50分35秒

i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg -- +1 on the cat photo

2019年05月24日50分35秒

Does this code close the file properly when the script ends or would it lose data?

2019年05月24日50分35秒

quantumpotato Take a look at the response you're getting back from your request

2019年05月24日50分35秒

This depends upon the req url type if you are requesting https you must use https otherwise it will throw error.

2019年05月24日50分35秒

TheGrayFox Noone likes error handling :)

2019年05月24日50分35秒

vince-yuan is download() itself pipeable?

2019年05月24日50分35秒

Abdul Sounds like you are very new to node.js/javascript. Take a look at this tutorial: tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm It's not complex.

2019年05月24日50分35秒

Abdul maybe it would be good if you share with the rest of the class what you have figured out ?

2019年05月24日50分35秒

Is there a way to see the speed of the download? Like can track how many mb/s? Thanks!

2019年05月24日50分35秒

the callback is confusing me. if I now invoke download(), how would I do it? What would I place as the cb argument? I have the download('someURI', '/some/destination', cb) but don't understand what to put in the cb

2019年05月23日50分35秒

Abdul You specify the callback with a function only if you need to do something when the file has been successfully fetched.

2019年05月24日50分35秒

The request module just works straight for HTTPs. Cool!

2019年05月23日50分35秒

ventura yep, btw, there's also the native https module that now can handle secure connections.

2019年05月24日50分35秒

I had tried before, but didn't work properly.

2019年05月24日50分35秒

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.

2019年05月23日50分35秒

Alex, nope, this is an error message and there's a return. So if response.statusCode !== 200 the cb on finish will never be called.

2019年05月24日50分35秒

What for are you storing request into a variable?

2019年05月24日50分35秒

he "stores" it into a variable so it does not become a global variable by default.

2019年05月23日50分35秒

philk how do you know a global variable is created if var request = is removed?

2019年05月23日50分35秒

You are right, there is no need to save the request, its not used anyway. That's what you mean?

2019年05月23日50分35秒

responseSet flag caused, for some reason which I hadn't had the time to investigate, my file to be downloaded incompletely. No errors popped up but the .txt file I was populating had half of the rows that needed to be there. Removing the logic for the flag fixed it. Just wanted to point that out if someone had the issues with the approach. Still, +1

2019年05月24日50分35秒

Two comments on this: 1) it should probably reject Error objects, not strings, 2) fs.unlink will quietly swallow errors which might not necessarily be what you want to do

2019年05月23日50分35秒

this is only a file, has no protocol or server to download from... http.get("http://example.com/yourfile.html",function(){})

2019年05月24日50分35秒

Is there a memory leak in this answer: stackoverflow.com/a/22793628/242933?

2019年05月24日50分35秒

You can add timeout like I did in http.get. The memory leak is only if the file take too long to be downloaded.

2019年05月23日50分35秒

It is returning garbage character if file name is other than ascii like if filename is in japanese.

2019年05月23日50分35秒

Do you think ajax-request is not a third party library?

2019年05月24日50分35秒

302 is also HTTP status code for URL redirect, so you should use this [301,302].indexOf(res.statusCode) !== -1 in the if statement

2019年05月23日50分35秒

The questions was specific to not include third party modes :)

2019年05月23日50分35秒

Code dumps are generally not useful and may be downvoted or deleted. It would be worth editing to at least explain what the code is doing for future visitors.

2019年05月24日50分35秒

request seems to be a third-party library.

2019年05月24日50分35秒

The question is asking how to do it "without using third-party libraries".

2019年05月23日50分35秒