错误:无法加载运行app.js的脚本

时间:2019-02-17 14:32:30

标签: javascript node.js html5 express

我有一个简单的脚本,可以通过服务器端的node.js运行CCapture.js

这是app.js

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require("fs");
const _ = require("lodash");

const { window } = new JSDOM(
  `
<body>
   <script src='./node_modules/ccapture.js/build/CCapture.all.min.js'></script>
    <canvas id='animation' width='400' height='200'></canvas>
</body>
`,
  // We need these options to allow JSDOM to require CCapture from node_modules
  { runScripts: "dangerously", resources: "usable" }
);

const document = window.document;

// Do our stuff after DOM is ready.
window.onload = () => {
  const canvas = document.getElementById("animation");
  const ctx = canvas.getContext("2d");

  // Doing some random animation here
  const render = () => {
    ctx.fillStyle = "blue";
    ctx.font = "30px Impact";
    ctx.rotate(_.random(0.1, 0.2));
    ctx.fillText("Awesome!", 50, 100);
  };

  // Framerate for capturer is 1 per second just for example
  const capturer = new window.CCapture({
    format: "png",
    framerate: 1,
    verbose: true
  });

  capturer.start();

  // Doing 3 renders, and capture the canvas
  const interval = setInterval(() => {
    render();
    capturer.capture(canvas);
  }, 1000);

  // Now clearing the interval, stopping capturer
  setTimeout(() => {
    clearInterval(interval);

    capturer.stop();

    // Saving the file using FileReader (from JSDOM) and node.js API
    capturer.save(blob => {
      const reader = new window.FileReader();
      reader.onload = () => {
        const arrayBuffer = reader.result;
        const uint8Array = new Uint8Array(arrayBuffer);

        // Sync for simplicity
        fs.writeFileSync("./images.tar", uint8Array, { encoding: "binary" });
      };

      reader.readAsArrayBuffer(blob);
    });
  }, 4000);
};

不幸的是,当我运行node app.js时,出现以下错误。

Error: Could not load script: "./node_modules/ccapture.js/build/CCapture.all.min.js"

我在这里浏览我的网站

.
├── app.js
├── css
│   └── main.css
├── index.html
├── js
│   ├── CCapture.all.min.js
│   └── main.js
├── node_modules
    ├── ccapture.js
    │   │   ├── bower.json
    │   │   ├── build
    │   │   │   ├── CCapture.all.min.js
    │   │   │   └── CCapture.min.js
    │   │   ├── LICENSE
    │   │   ├── package.json
    │   │   ├── README.md
    │   │   └── src
    │   │       ├── CCapture.js
    │   │       ├── download.js
    │   │       ├── gif.js
    │   │       ├── gif.worker.js
    │   │       ├── tar.js
    │   │       ├── webm-writer-0.2.0.js
    │   │       └── Whammy.js

    ........

我的代码有什么问题?

0 个答案:

没有答案