You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Catherine Jones <ca...@gmail.com> on 2010/09/01 20:02:24 UTC

list function "head" and "req" arguments

Hi, 

I'm looking for documentation re the "head" and "req" arguments supplied
to list functions (as mentined in the _Definitive Guide_.) It's not
clear to me how to extract information from them. My specific need is as
follows:

I have a couch database of documents with attached images. I have a
view with a map function that emits pairs of the form:

(doc_id, name_of_attached_image)

And I have a list function that assembles these images into a web page:

function(head, req) {
    provides('html', 
              function(){
                  var s = '<!DOCTYPE html><html><body>'; 
                  while (row = getRow()) 
                      s += '<img src=\"http://127.0.0.1:5984/db/' +\
                           row['value'][0] + '/' + \
                           row['value'][1] + '\" />'; 
                  s += '</body></html>';
                  send(s);})}

Everything works fine locally, but if I want the same functionality
on my remote server (where 5984 is bound to 0.0.0.0 rather than
127.0.0.1) I need to replace 127.0.0.1 in the list function with some
variable that evaluates to the name of the host being queried. I'm
guessing that the hostname can be extracted somehow from the head or
req arguments of the list function, but need a pointer to some info.

Thanks,

Catherine





Re: list function "head" and "req" arguments

Posted by Catherine Jones <ca...@gmail.com>.
Thanks, Chris - worked perfectly. Including log(req) and log(head)
statements in the list function gave a wealth of information about the
function's "head" and "req" arguments.

In particular, the name of the host being queried can be extracted from
the "req" argument as req['headers']['Host'].

Catherine


Re: list function "head" and "req" arguments

Posted by J Chris Anderson <jc...@apache.org>.
On Sep 1, 2010, at 11:23 AM, Catherine Jones wrote:

> Thanks, Aurelien. I'll try that approach. (I'd got confused before
> working with relative urls by the extra slashes in the
> _design/design_doc and _list/list_name parts of the path.) But I'm still
> curious about those req and head arguments. (Maybe just my ignorance of
> JavaScript - need to learn about how to inspect JS objects....)
> 

there is a log() function you can call from inside the query server which will print the inspection to the couch.log file. that should give you what you need.

Cheers,
Chris

> Catherine
> 
> On Wed, 2010-09-01 at 20:12 +0200, Aurélien Bénel wrote:
>> Hi Catherine,
>> 
>>> I'm guessing that the hostname can be extracted somehow from the head or req arguments of the list function
>> 
>> I didn't find the hostname in the arguments either...
>> However, in your case, why don't you use relative URLs?
>> 
>> 
>> Regards,
>> 
>> Aurélien
> 


Re: list function "head" and "req" arguments

Posted by Catherine Jones <ca...@gmail.com>.
Thanks, Aurelien. I'll try that approach. (I'd got confused before
working with relative urls by the extra slashes in the
_design/design_doc and _list/list_name parts of the path.) But I'm still
curious about those req and head arguments. (Maybe just my ignorance of
JavaScript - need to learn about how to inspect JS objects....)

 Catherine

On Wed, 2010-09-01 at 20:12 +0200, Aurélien Bénel wrote:
> Hi Catherine,
> 
> > I'm guessing that the hostname can be extracted somehow from the head or req arguments of the list function
> 
> I didn't find the hostname in the arguments either...
> However, in your case, why don't you use relative URLs?
> 
> 
> Regards,
> 
> Aurélien


Re: list function "head" and "req" arguments

Posted by Aurélien Bénel <au...@utt.fr>.
Hi Catherine,

> I'm guessing that the hostname can be extracted somehow from the head or req arguments of the list function

I didn't find the hostname in the arguments either...
However, in your case, why don't you use relative URLs?


Regards,

Aurélien