You can get the command-line arguments from the process. With that, you can reuse the download. Each time you execute the script, pass the required url and path arguments from the command-line. The example below will download the image from the first argument as save it as image. Sometimes, you may need to download multiple files and save them into your system.
The download package allows you to download multiple images and save them under a folder as shown below:. We are using spawn instead of exec for the sake of convenience - spawn returns a stream with data event and doesn't have buffer size issue unlike exec. That doesn't mean exec is inferior to spawn ; in fact we will use exec to download files using wget. The way data was written to the instance of fs. 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. The code above saves both logo. Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed. I started this blog as a place to share everything I have learned in the last decade.
The questions was specific to not include third party modes : — David Gatti. If you are using express use res. A 13k 4 4 gold badges 90 90 silver badges bronze badges. Using the http2 Module I saw answers using the http , https , and request modules. I'd like to add one using yet another native NodeJS module that supports either the http or https protocol: Solution I've referenced the official NodeJS API, as well as some of the other answers on this question for something I'm doing.
Rik Rik 3 3 silver badges 8 8 bronze badges. Is this backward compatible? Works everywhere or only for http2? Neil I'm assuming you are asking if http2 is backward compatible with http1. Because http2 improves framing; adding binary compression, the ability to push from server to client, and simultaneous connections - it is absolutely required that both server and client know the implementation this allows abstraction of implementation from the application too.
Good news is that all major browsers have supported http2 since about - and Node as a client does too. Node, Nginx, and Apache offer it server side - so most use cases are covered. Its a vast improvement. Thanks for the reply Rik — Neil. Without library it could be buggy just to point out.
Here my suggestion: Call system tool like wget or curl use some tool like node-wget-promise which also very simple to use. Geng Jiawen Geng Jiawen 7, 2 2 gold badges 41 41 silver badges 36 36 bronze badges. Writing my own solution since the existing didn't fit my requirements. Alex Pilugin Alex Pilugin 2 2 gold badges 9 9 silver badges 30 30 bronze badges. Pankaj Pankaj 1 1 gold badge 3 3 silver badges 9 9 bronze badges.
Frankenmint Frankenmint 1, 1 1 gold badge 17 17 silver badges 30 30 bronze badges. Like: res. Jeremy M. Yin Yin 9. I suggest you to use res. Mehrzad Tejareh 4 4 silver badges 18 18 bronze badges. Vivek Narayan Vivek Narayan 1 1 1 bronze badge.
Bugs 4, 9 9 gold badges 31 31 silver badges 40 40 bronze badges. Chandrakant Thakkar Chandrakant Thakkar 8 8 silver badges 21 21 bronze badges. 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. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast what if you could invest in your favorite developer? Who owns this outage? Building intelligent escalation chains for modern SRE.
Featured on Meta.
0コメント