1

我试图让 dojo 1.8 的 dojo/request 在 OS X 10.8.2 Mac 上的 node.js v0.8.17 下工作。

我尝试了来自dojotoolkit.org的两个示例,这两个示例都失败并出现相同的错误:

module.js:236
  var start = request.substring(0, 2);
                      ^
TypeError: Object require,dojo/request has no method 'substring'
    at Function.Module._resolveLookupPaths (module.js:236:23)
    at Function.Module._resolveFilename (module.js:328:31)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/chris/src/bsn/INF/spikes/dojo_1-8/example/example.js:12:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

这是基于dojo 教程的第一个示例:

// dojo-release-1.8.3-src is installed in the same directory as this script

dojoConfig = {
    baseUrl: ".",
    packages:[{name: 'dojo', location: 'dojo'}],
    deps:['dojo/request', 'dojo/date']
};

require('./dojo/dojo.js');

console.log("I can call dojo/date functions no problem:");
console.log("e.g. Time Zone is:" + dojo.date.getTimezoneName(new Date()));

console.log("But when I try to use dojo/request it fails:");

// Following taken from:
// http://dojotoolkit.org/documentation/tutorials/1.8/ajax/

require(["dojo/request"], function(request){
    request("helloworld.txt").then(
        function(text){
            console.log("The file's contents is: " + text);
        },
        function(error){
            console.log("An error occurred: " + error);
        }
    );
});

这是从dojo 参考指南中摘录的第二个:

// dojo-release-1.8.3-src is installed in the same directory as this script

dojoConfig = {
    baseUrl: ".",
    packages:[{name: 'dojo', location: 'dojo'}],
    deps:['dojo/request', 'dojo/date']
};

require('./dojo/dojo.js');

// Following is taken from:
// https://dojotoolkit.org/reference-guide/1.8/dojo/request/node.html#id5

require(['require', 'dojo/request'], function(require, request){
  var http = require.nodeRequire('http'),
    timeout;

  var server = http.createServer(function(request, response){
    var body = '{ "foo": "bar" }';
    response.writeHead(200, {
      'Content-Length': body.length,
      'Content-Type': 'application/json'
    });
    response.write(body);
    response.end();
  });

  server.on('close', function(){
    if(timeout){ clearTimeout(timeout); }
  });

  server.on('listening', function(){
    request.get('http://localhost:8124', {
      handleAs: 'json',
      headers: { 'Range': '1-2' },
      timeout: 1000
    }).then(function(data){
      console.log(data);
      server.close();
    }, function(err){
      console.log(err);
      server.close();
    });
  });

  server.listen(8124);
});

我不明白为什么在第一个示例中对 dojo.date 的调用可以正常工作,但 dojo.require 却不行。

有任何想法吗?

谢谢

克里斯

4

1 回答 1

0

因此,多亏了dojo-interest邮件列表上的几个线程、bpayton、Kenneth G. Franqueiro 和 kitsonk 的支持,我能够解决这个问题。

为了让 dojo/request 工作,您需要从模块中要求它,在这种情况下,我将其称为“helloworld.js”,因为它正在请求一个名为“helloworld.txt”的文档:

// Following taken from:
// http://dojotoolkit.org/documentation/tutorials/1.8/ajax/

require(["dojo/request"], function(request){
    request("helloworld.txt").then(
        function(text){
            console.log("The file's contents is: " + text);
        },
        function(error){
            console.log("An error occurred: " + error);
        }
    );
});

然后将此模块包含在您的 dojoConfig deps 数组中:

// dojo-release-1.8.3-src is installed in the same directory as this script

dojoConfig = {
    baseUrl: ".",
    packages:[{name: 'dojo', location: 'dojo'}],
    deps:['dojo/request', 'dojo/date', 'helloworld.js']
};

require('./dojo/dojo.js');

console.log("I can call dojo/date functions no problem:");
console.log("e.g. Time Zone is:" + dojo.date.getTimezoneName(new Date()));

console.log("And now when I try to use dojo/request it works:");

现在从节点运行它给了我:

arnosgrove:example chris$ node useRequest.js 
I can call dojo/date functions no problem:
e.g. Time Zone is:GMT
And now when I try to use dojo/request it works
The file's contents is: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

这是我希望看到的。

于 2013-01-29T18:20:56.007 回答