How to download a file with Node.js (without using third-party libraries)?

后端 未结 28 1993
逝去的感伤
逝去的感伤 2020-11-22 03:37

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 giv

28条回答
  •  暖寄归人
    2020-11-22 03:55

    Hi,I think you can use child_process module and curl command.

    const cp = require('child_process');
    
    let download = async function(uri, filename){
        let command = `curl -o ${filename}  '${uri}'`;
        let result = cp.execSync(command);
    };
    
    
    async function test() {
        await download('http://zhangwenning.top/20181221001417.png', './20181221001417.png')
    }
    
    test()
    

    In addition,when you want download large、multiple files,you can use cluster module to use more cpu cores.

提交回复
热议问题