Fork me on GitHub

diet 0.9

Signal: Response

# Intro

Node.js by default has two arguments request and response when creating a HTTP(s) server using http.createServer(). Diet combines these two objects into a single Signal Object defined as a dollar sign $


This document describes the Signal Methods that are inherited from the Response Object.

# $.end(data) Function

A function that ends the response and send back data to the requesting device.

data string or object or buffer or integer

The message to respond with to the client.

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "Hello world!"
app.get('/', function($){
   $.end('Hello world!')
})

# $.send(data) Function

A function that writes to the response stream.

data string or object or buffer or integer

The message to respond with to the client.

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "Hello world!"
app.get('/', function($){
   $.send('Hello ')
   $.send(' world!')
   $.end()
})

# $.header(name, value) Function

Get or Set Headers. The second argument is for SET, if left empty it's GET.

name string

The name of the header.

value string optional

Set the value of the header named by the name attribute.

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET /
app.get('/', function($){
    $.header('cookie') // GET `cookie` -> 'id=999'
    $.header('content-type', 'text/html') // SET `content-type` to `text/html`
})
  

# $.data Object

Response data used by $.json() responses and template engines for html, email etc.

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "index.html"
app.get('/', function($){
   $.data.hello = 'world'
   $.json()
})

// when "GET http://localhost:8000/user/create" is requested
// the response is the following:
{ "hello": "world" }

# $.status(code, message) Function

Set the status of the response message.

code integer

Status Code. Sets $.statusCode

message string optional

Optional Status Message. Sets $.statusMessage

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "index.html"
app.get('/', function($){
   $.status('404', 'Page Not Found')
   $.end($.statusCode + ' ' + $.statusMessage) // 404 Page Not Found
})

# $.redirect(path, statusCode) Function

Redirect the request to a different Path or URL.

path string

Redirect to this Path or URL.

statusCode integer optional

Optional Status Code.

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// internal redirect 
app.get('/one', function($){
   $.redirect('/to/some/path') 
})

// external redirect
app.get('/two', function($){
   $.redirect('http://google.com/')
})

// redirect home
app.get('/three', function($){
   $.redirect('home') 
})

// redirect back
app.get('/four', function($){
   $.redirect('back') 
})

# $.json(data) Function

End a response with a JSON Object. Inherits the properties of $.data

data object

the json object

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with a JSON Object
app.get('/', function($){
   $.json({ hello: 'world' }) // outputs: { hello: 'world' }
})

# $.success() Function

End a response with a JSON Object. Sets $.data.passed and $.passed to true. Inherits the properties of $.data

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with a JSON Object
app.get('/', function($){
   $.success() // outputs: { passed: true }
})

# $.failure() Function

End a response with a JSON Object. Sets $.data.passed and $.passed to false. Inherits the properties of $.data

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "index.html"
app.get('/myPath', function($){
   if(1+1 == 3){
       $.failure()
   } 
})

// when "GET http://localhost:8000/myPath" is requested the response is:
{ passed: false }

# $.error(field, message) Function

Register an error into $.errors. Sets $.data.passed and $.passed to false.

field string

the error index

message string

the error message

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "index.html"
app.get('/myPath', function($){
   $.error('myField', 'invalid value')
   $.failure()
})

// when "GET http://localhost:8000/myPath" is requested the response is:
{ passed: false, errors: { myField: 'invalid value' } }

# $.errors Object

If not empty it is appended to $.data as $.data.errors

// Create an app
var server = require('diet')
var app = server()
app.listen('http://localhost:8000')

// Listen on GET / and respond with "index.html"
app.get('/', function($){
   $.errors.myField = 'invalid value'
   $.failure()
})


// when "GET http://localhost:8000/myPath" is requested the response is:
{ passed: false, errors: { myField: 'invalid value' } }