http - 带有 WGET 的 NodeJS 文件下载器?

标签 http node.js download exec wget

我正在尝试使用 nodejs 的 WGET 方法进行文件下载。我发现了这个:

var exec = require('exec');

// Function to download file using wget
var download_file_wget = function(file_url) {

    // extract the file name
    var file_name = url.parse(file_url).pathname.split('/').pop();
    // compose the wget command
    var wget = 'wget -P ' + DOWNLOAD_DIR + ' ' + file_url;
    // excute wget using child_process' exec function

    var child = exec(wget, function(err, stdout, stderr) {
        if (err) throw err;
        else console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR);
    });
};

但是它说:

Error: Cannot find module 'exec'

exec 是另一个要安装和导入的模块吗?或者我怎样才能让它工作?

最佳答案

是的,url是内置 Node 模块之一

就这样

var url = require('url');

在你的文件中的某处。

execchild_process 的一部分所以要做到这一点

var exec = require('child_process').exec;

关于http - 带有 WGET 的 NodeJS 文件下载器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11112466/

相关文章:

java - 拦截传出的 HTTP 请求

php - Socket.io私信通知

node.js - 在 Azure Functions 上使用 ImageMagick 或 GraphicsMagick

file-upload - XPages POI4Xpages 下载到网络位置

ruby - 如何使用修改后的 header 制作 HTTP GET?

sockets - Flutter 应用程序无法与证书握手错误 CERTIFICATE_VERIFY_FAILED 建立套接字连接

c++ - 使用 libmicrohttpd 处理 POST 请求

javascript - request(...).then 在发出 POST 请求时不是函数错误

android - 是否可以在 Android DownloadManager 中提交 cookie

javascript - IE8 的信息栏阻止脚本文件下载以响应 JQuery AJAX 请求