Nodejs在`new <something>`声明?</something>上失败

时间:2015-03-15 00:31:08

标签: node.js jsdom

当客户端

时,我的js功能正常运行
// [...] (some meta lines here)
var converterEngine = function (input) { // BLOB => Binary => Base64
    var uInt8Array = new Uint8Array(input),
          i = uInt8Array.length;
    var biStr = []; //new Array(i);
    while (i--) { biStr[i] = String.fromCharCode(uInt8Array[i]);  }
    var base64 = window.btoa(biStr.join(''));
    return base64;
};
// less important but complementary
var getImageBase64 = function (url, callback) {
    // to comment better
    var xhr = new XMLHttpRequest(url), img64;
    xhr.open('GET', url, true); // url is the url of a PNG/JPG image.
    xhr.responseType = 'arraybuffer';
    xhr.callback = callback;
    xhr.onload  = function(){
        img64 = converterEngine(this.response); // convert BLOB to base64
        this.callback(null,img64); // callback : err, data
    };
    xhr.onerror = function(){ callback('B64 ERROR', null); };
    xhr.send();
};

在nodejs端,代码通过node myscript.js加载和使用,例如:

var jsdom = require('jsdom');
var fs    = require('fs');

jsdom.env(
  "<html><body></body></html>",        // CREATE DOM HOOK:
  [ '../js/b64.js' ],                 // ... & offline

  function (err, window) {
   window.getImageBase64("http://bip.bop/img.png", function(err,data){
              //doing more funny stuff here
           })
  }
);

通过节点运行时,newTypeError: Type error的nodejs脚本失败:

file:///data/yug/projects_active/make-modules/js/b64.js:9
    var uInt8Array = new Uint8Array(input),
                     ^
TypeError: Type error
    at converterEngine (file:///data/yug/projects_active/make-modules/js/b64.js:9:22)
    at xhr.onload (file:///data/yug/projects_active/make-modules/js/b64.js:24:17)
    at dispatchEvent (/data/yug/projects_active/make-modules/node_modules/jsdom/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:570:25)
    at setState (/data/yug/projects_active/make-modules/node_modules/jsdom/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:593:14)
    at IncomingMessage.<anonymous> (/data/yug/projects_active/make-modules/node_modules/jsdom/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:427:13)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

在nodejs中使用new是否有任何限制?如何使其在客户端和服务器端都能正常工作?

0 个答案:

没有答案