Egret入门学习日记 --- 第六十四篇(书中 19.4 节 内容)

第六十四篇(书中 19.4 节 内容)

  昨天的问题,是 images 库自己本身的问题。

  我单独使用都报错。

  这是main.js文件代码:

let images = require("images");

console.log(images);

  这是cmd运行命令历史:

Microsoft Windows [版本 10.0.16299.15]
(c) 2017 Microsoft Corporation。保留所有权利。

C:\Users\Administrator\Desktop\a>node mian.js
module.js:550
    throw err;
    ^

Error: Cannot find module 'C:\Users\Administrator\Desktop\a\mian.js'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

C:\Users\Administrator\Desktop\a>node main.js
C:\Users\Administrator\Desktop\a\node_modules\images\scripts\util\binding.js:20
      throw new Error(errors.missingBinary());
      ^

Error: Missing binding C:\Users\Administrator\Desktop\a\node_modules\images\vendor\win32-x64-57\binding.node
images could not find a binding for your current environment: Windows 64-bit with Node.js 8.x

Found bindings for the following environments:
  - Windows 64-bit with Node.js 8.x

This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild images --force` to build the binding for your current environment.
    at module.exports (C:\Users\Administrator\Desktop\a\node_modules\images\scripts\util\binding.js:20:13)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\a\node_modules\images\index.js:32:51)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\a\main.js:1:76)

C:\Users\Administrator\Desktop\a>npm i
npm WARN new@1.0.0 No description
npm WARN new@1.0.0 No repository field.

audited 285 packages in 2.095s
found 0 vulnerabilities


C:\Users\Administrator\Desktop\a>node main.js
C:\Users\Administrator\Desktop\a\node_modules\images\scripts\util\binding.js:20
      throw new Error(errors.missingBinary());
      ^

Error: Missing binding C:\Users\Administrator\Desktop\a\node_modules\images\vendor\win32-x64-57\binding.node
images could not find a binding for your current environment: Windows 64-bit with Node.js 8.x

Found bindings for the following environments:
  - Windows 64-bit with Node.js 8.x

This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild images --force` to build the binding for your current environment.
    at module.exports (C:\Users\Administrator\Desktop\a\node_modules\images\scripts\util\binding.js:20:13)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\a\node_modules\images\index.js:32:51)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\a\main.js:1:76)

C:\Users\Administrator\Desktop\a>npm rebuild images --force
npm WARN using --force I sure hope you know what you are doing.

> images@3.0.2 install C:\Users\Administrator\Desktop\a\node_modules\images
> node ./scripts/install.js

Downloading binary from https://github.com/zhangyuanwei/node-images/releases/download/v3.0.2/win32-x64-57_binding.node
Cannot download "https://github.com/zhangyuanwei/node-images/releases/download/v3.0.2/win32-x64-57_binding.node":

ESOCKETTIMEDOUT

Hint: If github.com is not accessible in your location
      try setting a proxy via HTTP_PROXY, e.g.

      export HTTP_PROXY=http://example.com:1234

or configure npm proxy via

      npm config set proxy http://example.com:8080
images@3.0.2 C:\Users\Administrator\Desktop\a\node_modules\images

C:\Users\Administrator\Desktop\a>node main.js
module.js:682
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: \\?\C:\Users\Administrator\Desktop\a\node_modules\images\vendor\win32-x64-57\binding.node is not a valid Win32 application.
\\?\C:\Users\Administrator\Desktop\a\node_modules\images\vendor\win32-x64-57\binding.node
    at Object.Module._extensions..node (module.js:682:18)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at module.exports (C:\Users\Administrator\Desktop\a\node_modules\images\scripts\util\binding.js:24:10)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\a\node_modules\images\index.js:32:51)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)

C:\Users\Administrator\Desktop\a>

  那我就不理会这个库了,反正只是做一些图片的裁剪而已。

  

  开始 19.4节。

  

扫描二维码关注公众号,回复: 7260388 查看本文章

  

   重点:

    1、通过谷歌提供Har功能,保存 har 文件。

    2、解析HAR文件。

    3、编写代码,下载对应文件。

  操作:

    1、通过谷歌提供Har功能,保存 har 文件。

      

      

    2、解析HAR文件。

      

    3、编写代码,下载对应文件。

      上代码(我写了很详细的注释了):

        let jsonStr = fs.readFileSync('./developer.egret.com.har', 'utf-8');
            let obj = JSON.parse(jsonStr);
            let array: Array<any> = obj.log['entries'];
            let totalNum = 0;

            //资源列表
            let resList = [];
            for (let i = array.length - 1; i >= 0; i--) {

                //由于 node.js 的 http 模块 不支持 https 协议,所以要把 https 协议 过滤掉。
                if ((<string>array[i].request.url).indexOf("https:") != -1) {
                    continue;
                }

                let resItem = {
                    url: array[i].request.url,
                    time: 0
                };
                resList.push(resItem);
            }

            initLodar(resList);

            /** 解析资源,并下载 */
            function initLodar(resList: Array<any>) {
                //下载后要保存到的文件夹名
                let downSaveDirName = "download";
                if (fs.existsSync(downSaveDirName) == false) {
                    fs.mkdirSync(downSaveDirName);
                }
                console.log("正在下载资源...文件总数为: " + resList.length);
                resList.forEach((item, index, array) => {
                    startDownLoadResItem(item, downSaveDirName);
                });

            }
            /** 使用nodejs 做http的请求 
             * @param item 下载对象
             * @param downSaveDirName 下载后要保存到的文件夹名
            */
            function startDownLoadResItem(item, downSaveDirName) {
                let req = http.request(item.url, getReqCallback(item, downSaveDirName));
                req.on('error', (e) => {
                    if (item.time < 3) {
                        console.log("资源" + item.url + "错误,再次请求");
                        item.time++;
                        startDownLoadResItem(item, downSaveDirName);
                    } else {
                        console.log("该资源已经请求超过三次,但还是下载失败。");
                    }
                });

                req.end();
            }
            /** http请求的回调函数 
             * @param item 下载对象
             * @param downSaveDirName 下载后要保存到的文件夹名
            */
            function getReqCallback(item, downSaveDirName) {
                /** 获取 http://www.hao.com/aa/bb.png 中的 bb.png */
                let fileName = path.basename(item.url);

                let tempName: string = item.url;

                /** 获取 http://www.hao.com/aa/bb.png 中的 aa/bb.png */
                let writeName = tempName.substring(tempName.indexOf(".")); //获取 http://www.hao.com/aa/bb.png 中的 .hao.com/aa/bb.png
                writeName = writeName.substring(writeName.indexOf("/") + 1); //获取 .hao.com/aa/bb.png 中的 aa/bb.png

                /** http请求后触发的回调
                 * @param res 请求后得到的数据
                 */
                function handler(res) {
                    /** 获取资源字节长度 */
                    let contentLength = parseInt(res.headers['content-length']);

                    /** 请求到的所有 buff 数据 */
                    let fileBuff = [];
                    res.on('data', function (chunk) {
                        let buffer = new Buffer(chunk);
                        fileBuff.push(buffer);
                    });

                    res.on('end', function () {
                        if (isNaN(contentLength)) {
                            console.log(item.url + "资源下载发生错误");
                            return;
                        }

                        /** 合并所有分段的 buff 数据 */
                        let totalBuff = Buffer.concat(fileBuff);

                        if (totalBuff.length < contentLength) {
                            console.log(item.url + "资源字节书不符,再次启动加载");
                            startDownLoadResItem(item, downSaveDirName);
                            return;
                        }

                        writeFile(downSaveDirName + "\\" + writeName, totalBuff, downSaveDirName, writeName, fileName);
                        
                    });

                }
                let callback = handler;
                return callback;

            }
            /** 保存成文件
             * @param url 最终文件保存的目录
             * @param buffer 要保存的数据
             * @param dirName 保存文件的所在文件夹名
             * @param writeName 排除域名后的文件路径。如:获取 http://www.hao.com/aa/bb.png 中的 aa/bb.png
             * @param fileName 保存的文件名
            */
            function writeFile(url, buffer, dirName, writeName, fileName) {

                /** 保存的文件所在的文件夹名 */
                let tempDir = dirName;

                /** 获取 http://www.hao.com/aa/bb.png 中的 aa/bb.png */
                let tempPath = writeName;

                /** 获取 aa/bb.png 中的 aa */
                let targetPath = writeName.substring(0, writeName.lastIndexOf("/"));

                //检查文件是否存在,如果不存在则 递归创建文件夹
                // if (fs.existsSync(tempDir + targetPath) == false) { // 如果 downloadaa 不存在,这递归创建。。。为什么要判断 downloadaa ? 不是应该要判断 download 而已吗?我先注释吧
                if (fs.existsSync(tempDir + "\\" + targetPath) == false) { // 感觉书中应该是漏写了 \\ 吧,我就判断 download/aa 是否存在,如果不存在则开始创建递归创建文件夹
                    createFloader(tempDir, tempPath);
                }

                //开始保存文件
                fs.writeFile(url, buffer, (err) => {
                    if (err != null) {
                        console.log("写入文件错误" + dirName + "/" + writeName);
                    } else {
                        totalNum++;
                        console.log("当前数量" + totalNum + "写入文件成功:" + fileName);
                    }
                });
            }
            /** 递归创建文件夹 
             * @param downSaveDirName 下载后要保存到的文件夹名
             * @param targetPath 剩下的文件路径。如:获取 http://www.hao.com/aa/bb.png 中的 aa/bb.png
            */
            function createFloader(downSaveDirName, targetPath: string) {
                if (targetPath.indexOf("/") == -1) return;

                /** aa/bb.png 中的 aa  */
                let temp = targetPath.substring(0, targetPath.indexOf("/"));
                targetPath = targetPath.substr(targetPath.indexOf("/") + 1); //获取 aa/bb.png 中的 bb.png
                downSaveDirName = downSaveDirName + "\\" + temp; // download\\aa  

                //文件夹不存在则创建一个  
                if (fs.existsSync(downSaveDirName) == false) fs.mkdirSync(downSaveDirName);

                //如果还有子文件夹,则继续创建
                if (targetPath.indexOf("/") != -1) return createFloader(downSaveDirName, targetPath);

            }

      下载成功了:

      

  至此,19.4节 结束。

  吃饭。

猜你喜欢

转载自www.cnblogs.com/dmc-nero/p/11505924.html