librelist archives

« back to archive

Custom response body/status/headers

Custom response body/status/headers

From:
Ash Berlin
Date:
2010-03-01 @ 13:43
Hey Basant,

Saw your question in IRC but you weren't about by the time I noticed: 
"need to know how to output non-template response on juice"

If you upgrade to the latest Juice master from github 
<http://github.com/ashb/juice> then you can do something like this:

function my_controller() {
  this.res.body = string_or_blob;
  this.res.status = 200; // This is the default
  this.res.headers['content-type'] = 'text/html; charset=utf-8';
}

"this.res" is an alias to "this.response" but I personally prefer the 
shorter version most of the time :)

Next thing is the changes we've made between 0.1 and this version (which 
will be 0.2 when we get the time). Hopefully the changes I committed last 
night should mean we are backwards compatible with 0.1, but I might have 
missed something.

- Juice.Application constructor should now be handed the "module" variable
from app.js (this is part of the module system from commonJS and is a 
nicer way of doing the DOC_ROOT feature we hacked about previously)
  + As a result app#setup no longer needs the docRoot param.
  + If you call Juice.Application's ctor without any arguments it will go 
into back-compat mode, issue a warning and then install shims to make 
things work.

- models can now be classes that are instantiated at setup time: 
http://github.com/ashb/juice/blob/00e49949b8/skeleton/lib/app.js#L9-20
  + Doing this means we can get rid of global variables in the example 
(the db handle for instance)
  + If a model is a constructor, it is created at setup time and passed 
the `app` variable.

We'll write some better docs for what you can do with the response object for 0.2.

-ash