Skip to content

Response

Mathias Quintero edited this page Mar 18, 2016 · 1 revision

Responses

Just like with the Requests, you also get special Response objects with their only simplifications.

Respond

There are many ways you can quickly respond. Status and headers are always optional and the defaults will be used if you don't specify them.

JSON

var myObject = {
  name: "My Object",
  id: 42
};
response.respondJSON(myObject);

Or if you want to include more information like the header:

var headers = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Methods': 'GET, POST',
  'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
}
response.respondJSON(myObject, 201, headers);

Plain Text

The same goes for when responding only with text:

response.respondPlainText("This is my response: 42");

A File

Simply call the function with the path to the file. You are not allowed to put your own headers on this one.

response.respondFile("path/to/file.ext");

Prompt for password

If you think the user forgot to put the auth info on there request you can prompt them again:

response.promptPassword("Please Enter a Password");

Redirect the user to somewhere else

You can also simply redirect the user to another place with, for example:

response.redirect("http://www.google.com");

Get The Original Object

We can also give you the regular response object that if you need something more tailored or down at the metal.

var res = response.getResponseObject();