You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Anh <7z...@gmail.com> on 2010/04/15 10:17:25 UTC
List functions and headers using send, getRow
Hi,
I'm a little confused on how to set the content-type header for list
functions which return HTML.
I'm using the send() and getRow() functions:
function(head, req) {
send('<html><body><ul>');
var row;
while (row = getRow()) {
send('<li>' + row.id + '</li>');
}
send('</ul></body></html>');
}
which works fine, but I'm not setting any content-type headers.
The examples I see in the docs use return, which returns the entire
body as well:
return {
"headers" : {"Content-Type" : "application/xml"},
"body" : new XML('<xml><node foo="bar"/></xml>')
}
Do I have to build the whole body as a string first, and then return it?
If so, that would seems to lose a benefit of list functions, that you
can process and send each row at a time, versus eating memory.
Thanks, any help would be appreciated.
Re: List functions and headers using send, getRow
Posted by Anh <7z...@gmail.com>.
Ah- thanks for the tip!
On Apr 21, 2010, at 6:53 AM, Steve Foulkes <sf...@fnal.gov> wrote:
> Hi,
>
> You can use the start() function like this:
> start({"headers": {"Content-Type": "text/html"}});
>
> Unfortunately, the only place I could find this documented was in
> the couch unit tests :-(
>
> Steve
>
> Anh wrote:
>> Hi,
>>
>> I'm a little confused on how to set the content-type header for list
>> functions which return HTML.
>> I'm using the send() and getRow() functions:
>>
>> function(head, req) {
>> send('<html><body><ul>');
>> var row;
>> while (row = getRow()) {
>> send('<li>' + row.id + '</li>');
>> }
>> send('</ul></body></html>');
>> }
>>
>> which works fine, but I'm not setting any content-type headers.
>>
>> The examples I see in the docs use return, which returns the entire
>> body as well:
>>
>> return {
>> "headers" : {"Content-Type" : "application/xml"},
>> "body" : new XML('<xml><node foo="bar"/></xml>')
>> }
>>
>>
>> Do I have to build the whole body as a string first, and then
>> return it?
>>
>> If so, that would seems to lose a benefit of list functions, that you
>> can process and send each row at a time, versus eating memory.
>>
>>
>> Thanks, any help would be appreciated.
>>
>>
>
Re: List functions and headers using send, getRow
Posted by Steve Foulkes <sf...@fnal.gov>.
Hi,
You can use the start() function like this:
start({"headers": {"Content-Type": "text/html"}});
Unfortunately, the only place I could find this documented was in the
couch unit tests :-(
Steve
Anh wrote:
> Hi,
>
> I'm a little confused on how to set the content-type header for list
> functions which return HTML.
> I'm using the send() and getRow() functions:
>
> function(head, req) {
> send('<html><body><ul>');
> var row;
> while (row = getRow()) {
> send('<li>' + row.id + '</li>');
> }
> send('</ul></body></html>');
> }
>
> which works fine, but I'm not setting any content-type headers.
>
> The examples I see in the docs use return, which returns the entire
> body as well:
>
> return {
> "headers" : {"Content-Type" : "application/xml"},
> "body" : new XML('<xml><node foo="bar"/></xml>')
> }
>
>
> Do I have to build the whole body as a string first, and then return it?
>
> If so, that would seems to lose a benefit of list functions, that you
> can process and send each row at a time, versus eating memory.
>
>
> Thanks, any help would be appreciated.
>
>
Re: List functions and headers using send, getRow
Posted by Anh <7z...@gmail.com>.
On Thu, Apr 15, 2010 at 9:48 AM, J Chris Anderson <jc...@gmail.com> wrote:
>
> On Apr 15, 2010, at 8:22 AM, Zachary Zolton wrote:
>
>> You can use the provides() helper function in your list _functions:
>>
>> function(head, req) {
>> provides('html', function() {
>> send('<html><body><ul>');
>> var row;
>> while (row = getRow()) {
>> send('<li>' + row.id + '</li>');
>> }
>> return '</ul></body></html>';
>> });
>> }
>>
>> The fun part is that you can use provides() multiple times to make the
>> same _list function handle multiple content types (i.e. html, xml,
>> atom) that result from requesting the URL with different Accept
>> headers.
>>
>
> Since most browsers are lousy at Accept headers you can also do query params like:
>
> /db/_design/foo/_list/bam?format=xml
>
> which provides will handle in an unsurprising way.
>
>
Thank you, works very nicely - I was thrown off by the examples using
"return {...}" versus send().
BTW, are there any online docs which allow you to click on various
functions and which contexts they are available?
I'm a Java guy, so thinking along the lines of online Javadocs, etc.
Thanks again
>> On Thu, Apr 15, 2010 at 3:17 AM, Anh <7z...@gmail.com> wrote:
>>> Hi,
>>>
>>> I'm a little confused on how to set the content-type header for list
>>> functions which return HTML.
>>> I'm using the send() and getRow() functions:
>>>
>>> function(head, req) {
>>> send('<html><body><ul>');
>>> var row;
>>> while (row = getRow()) {
>>> send('<li>' + row.id + '</li>');
>>> }
>>> send('</ul></body></html>');
>>> }
>>>
>>> which works fine, but I'm not setting any content-type headers.
>>>
>>> The examples I see in the docs use return, which returns the entire
>>> body as well:
>>>
>>> return {
>>> "headers" : {"Content-Type" : "application/xml"},
>>> "body" : new XML('<xml><node foo="bar"/></xml>')
>>> }
>>>
>>>
>>> Do I have to build the whole body as a string first, and then return it?
>>>
>>> If so, that would seems to lose a benefit of list functions, that you
>>> can process and send each row at a time, versus eating memory.
>>>
>>>
>>> Thanks, any help would be appreciated.
>>>
>
>
Re: List functions and headers using send, getRow
Posted by J Chris Anderson <jc...@gmail.com>.
On Apr 15, 2010, at 8:22 AM, Zachary Zolton wrote:
> You can use the provides() helper function in your list _functions:
>
> function(head, req) {
> provides('html', function() {
> send('<html><body><ul>');
> var row;
> while (row = getRow()) {
> send('<li>' + row.id + '</li>');
> }
> return '</ul></body></html>';
> });
> }
>
> The fun part is that you can use provides() multiple times to make the
> same _list function handle multiple content types (i.e. html, xml,
> atom) that result from requesting the URL with different Accept
> headers.
>
Since most browsers are lousy at Accept headers you can also do query params like:
/db/_design/foo/_list/bam?format=xml
which provides will handle in an unsurprising way.
> On Thu, Apr 15, 2010 at 3:17 AM, Anh <7z...@gmail.com> wrote:
>> Hi,
>>
>> I'm a little confused on how to set the content-type header for list
>> functions which return HTML.
>> I'm using the send() and getRow() functions:
>>
>> function(head, req) {
>> send('<html><body><ul>');
>> var row;
>> while (row = getRow()) {
>> send('<li>' + row.id + '</li>');
>> }
>> send('</ul></body></html>');
>> }
>>
>> which works fine, but I'm not setting any content-type headers.
>>
>> The examples I see in the docs use return, which returns the entire
>> body as well:
>>
>> return {
>> "headers" : {"Content-Type" : "application/xml"},
>> "body" : new XML('<xml><node foo="bar"/></xml>')
>> }
>>
>>
>> Do I have to build the whole body as a string first, and then return it?
>>
>> If so, that would seems to lose a benefit of list functions, that you
>> can process and send each row at a time, versus eating memory.
>>
>>
>> Thanks, any help would be appreciated.
>>
Re: List functions and headers using send, getRow
Posted by Zachary Zolton <za...@gmail.com>.
You can use the provides() helper function in your list _functions:
function(head, req) {
provides('html', function() {
send('<html><body><ul>');
var row;
while (row = getRow()) {
send('<li>' + row.id + '</li>');
}
return '</ul></body></html>';
});
}
The fun part is that you can use provides() multiple times to make the
same _list function handle multiple content types (i.e. html, xml,
atom) that result from requesting the URL with different Accept
headers.
On Thu, Apr 15, 2010 at 3:17 AM, Anh <7z...@gmail.com> wrote:
> Hi,
>
> I'm a little confused on how to set the content-type header for list
> functions which return HTML.
> I'm using the send() and getRow() functions:
>
> function(head, req) {
> send('<html><body><ul>');
> var row;
> while (row = getRow()) {
> send('<li>' + row.id + '</li>');
> }
> send('</ul></body></html>');
> }
>
> which works fine, but I'm not setting any content-type headers.
>
> The examples I see in the docs use return, which returns the entire
> body as well:
>
> return {
> "headers" : {"Content-Type" : "application/xml"},
> "body" : new XML('<xml><node foo="bar"/></xml>')
> }
>
>
> Do I have to build the whole body as a string first, and then return it?
>
> If so, that would seems to lose a benefit of list functions, that you
> can process and send each row at a time, versus eating memory.
>
>
> Thanks, any help would be appreciated.
>