1 | exports['text/html'] = (req, res, next) ->
|
2 | res.setHeader 'Content-Type', 'text/html'
|
3 | title = req.route.shortName || 'Un-named route'
|
4 | res.write "<html><head><title>#{title}</title></head><body>"
|
5 | writeHtml res, res.data
|
6 | res.end "</body></html>"
|
7 |
|
8 | writeHtml = (res, data) ->
|
9 | if Array.isArray(data) then htmlList res, data
|
10 | else if typeof data is 'undefined' then res.write '<em>undefined</em>'
|
11 | else if not data? then res.write '<em>null</em>'
|
12 | else if typeof data is 'object' then htmlDict res, data
|
13 | else if typeof data is 'string' then res.write data
|
14 | else if typeof data is 'number' then res.write data.toString()
|
15 | else
|
16 | console.log "Can't write this data as html"
|
17 | console.dir data
|
18 |
|
19 | htmlList = (res, ol) ->
|
20 | res.write '<ol>'
|
21 | for li in ol
|
22 | res.write '<li>'
|
23 | writeHtml res, li
|
24 | res.write '</li>'
|
25 | res.write '</ol>'
|
26 |
|
27 | htmlDict = (res, array) ->
|
28 | res.write '<dl>'
|
29 | for dt, dd of array
|
30 | res.write "<dt>#{dt}</dt>"
|
31 | res.write '<dd>'
|
32 | writeHtml res, dd
|
33 | res.write '</dd>'
|
34 | res.write '</dl>'
|
35 |
|
36 | exports['application/json'] = (req, res, next) ->
|
37 | res.setHeader 'Content-Type', 'application/json'
|
38 | res.end JSON.stringify res.data
|
39 |
|
40 |
|