Fork me on GitHub

diet 0.9

Signal: Request

# 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 Request Object.

# $.url Object

JSON Parsed request.url.

protocol string

The request/response protocol. It can be http: or https:

host string

The complete host of the requested resource including the port. It is the same as app.location.host

hostname string

The hostname of the requested resource exlucding the port. It is the same as app.location.hostname

port integer

The port. It is the same as app.location.port

href string

The full requested URL

path string

Everything after the host including the querystring.

pathname string

Everything after the host excluding the querystring.

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

// Listen on GET /user/:id
app.get('/', function($){
    console.log($.url)
    $.end()
})

// The above console.log outputs the following object 
// when the path http://localhost:8000/ is requested.
{  protocol: 'http:',
   slashes: true,
   host: 'localhost:8000',
   hostname: 'localhost',
   href: 'http://localhost:8000/',
   pathname: '/',
   path: '/'  }

# $.query Object

JSON parsed querystring from request.url.

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

// Listen on GET /
app.get('/', function($){
    console.log($.query)
    $.end()
})

// The above console.log outputs the following object 
// when the following path is requested: 
// GET http://localhost:8000/?string=hello&array=1&array=2&boolean=true 
{  string: 'hello',
   array: [1,2],
   boolean: true  }

# $.params Object

URL params from dynamic page routes like /user/:id.

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

// Listen on GET /user/:id
app.get('/user/:id', function($){
    console.log($.params)
    $.end()
})

// The above console.log outputs the following object 
// when the following path is requested: 
// GET http://localhost:8000/user/10 
{ id: 10 }

# $.body Object POST only

JSON Parsed post body.

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

// Listen on POST /user/create
app.post('/user/create', function($){
    console.log($.body)
    $.end()
})

// The above console.log outputs the following object 
// when this path is requested "POST http://localhost:8000/user/create"
// with the encoded body: "Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21"
{  name: 'Johnathan Doe',
   age: 23, 
   Formula: 'a + b == 13%!' }

# $.headers Object

An object of all headers.

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

// Listen on GET /
app.get('/', function($){
    console.log($.headers) 
})

// The above console.log outputs the following object 
{ host: 'localhost:8000',
  connection: 'keep-alive',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8,da;q=0.6,hu;q=0.4',
  cookie: 'id=999' }
  

# $.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`
})
  

# $.env Boolean

Shortcut for process.env.NODE_ENV

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

// Listen on GET /
app.get('/', function($){
    if($.env == 'development'){
    	$.end('development mode');
    } else {
    	$.end('production mode');
    }
})

# $.method String

Shortcut for request.method. It can be GET or POST. It is mostly useful in middleware functions.

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

app.get('/', function($){
    console.log($.method) // -> get
})

app.post('/', function($){
    console.log($.method) // -> post
})

# $.multipart Boolean

Shortcut for request.method. It can be GET or POST. It is mostly useful in middleware functions.

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

app.post('/', function($){
    if($.multipart){
    	// parse multipart form data	
    } else {
    	// just use $.body
    }
})