I am trying to get dojo 1.8's dojo/request to work under node.js v0.8.17 on an OS X 10.8.2 Mac.
I have tried two examples from dojotoolkit.org, both of which fail with the same error:
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)
Here is the first example based on the dojo tutorials:
// 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);
}
);
});
Here's the second one taken from the dojo reference guide:
// 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);
});
What I don't understand is why in the first example the calls to dojo.date work fine but not dojo.require.
Any ideas?
Thanks
Chris
So thanks to a couple of threads over on the dojo-interest mailing list, props to bpayton, Kenneth G. Franqueiro and kitsonk, I was able to solve this problem.
In order to get dojo/request to work you need to require it from a module, in this case I've called it 'helloworld.js' as it is requesting a doc called '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);
}
);
});
And then include this module in your dojoConfig deps array:
// 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:");
Running it from node now gives me:
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>
Which is what I'd expect to see.