incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Cottlehuber <...@jsonified.com>
Subject Re: CouchDB: Using XMLHttpRequest in Show Functions
Date Thu, 23 Jan 2014 14:19:38 GMT
> Mon, Jan 20, 2014 at 12:29 PM, Hank Knight <hknight555@gmail.com> 
> wrote: 
> I discovered that new functions can be made 
> available to all show and 
> list functions by modifying: 
> 
> /share/couchdb/server/main.js 
> 
> I have 
> attached an example. To test it, replace your version of 
> 
> "main.js" with the one I attached. 
> 
> Then 
> create a design document like the one I attached. 
> 
> 
> Test it like this: 
> http://localhost/db/_design/showtell/_show/test 
> 
> 
> This causes the custom function in main.js to be executed and 
> it 
> returns a result. 
> 
> I want 
> to use this method to retrieve data, like this: 
> 
> 
> function getPage(url) { 
> var req = new XMLHttpRequest(); 
> 
> req.open("GET", url, false); 
> req.send(null); 
> 
> return(req.responseText); 
> } 
> 
> 
> Everything works until I use XMLHttpRequest. Then I get an error: 
> 
> XMLHttpRequest is not defined 
> 
> How can 
> I gain access to XMLHttpRequest from main.js? 

Hi Hank,

There’s no support for running functions that have access to the outside world inside couchjs,
by design. This is both for security reasons (don’t want a random doc getting access to
the whole couchdb user’s filesystem), and also for sanity (result of a map on a doc should
be idempotent).

I suspect what you probably want is to use an external handler possibly running under nodejs,
functionally it will look the same but without the constraints you have:

https://couchdb.readthedocs.org/en/latest/config/externals.html and Paul’s excellent introduction https://couchdb.readthedocs.org/en/latest/externals.html
note that the latter was written in 2010 and the functionality has been included in couch
natively since then.

-- 
Dave Cottlehuber
Sent from my PDP11

Mime
View raw message