UNPKG

2.11 kBtext/coffeescriptView Raw
1lazorse = require '../'
2lazorse ->
3 greetingLookup = english: "Hi", french: "Salut"
4
5 # This defines a resource that accepts both GET and POST
6 @resource "/greeting/{language}":
7 description: "Per-language greetings"
8 shortName: 'localGreeting'
9 GET: -> @ok greeting: greetingLookup[@language], language: @language
10 POST: -> greetingLookup[@language] = @req.body; @ok()
11 examples: [
12 {method: 'GET', vars: {language: 'english'}}
13 {method: 'POST', vars: {language: 'english'}, body: "howdy"}
14 ]
15
16 # Define a coercion that restricts input languages to the
17 # ones we have pre-defined
18 @coerce "language", """
19 A language to use for localized greetings.
20 Valid values: #{Object.keys(greetingLookup).join(', ')}.
21 """, (language, next) ->
22 language = language.toLowerCase()
23 unless greetingLookup[language]
24 errName = @req.method is 'GET' and 'NotFound' or 'InvalidParameter'
25 @error errName, 'language', language
26 else
27 next null, language
28
29 # Extend the app with a custom rendering engine
30 # Uses https://github.com/visionmedia/consolidate.js
31 # and a non-standard resource property ``view``
32 cons = require 'consolidate'
33 @render 'text/html', (req, res, next) ->
34 res.setHeader 'Content-Type', 'text/html'
35 engine = req.resource.view?.engine or 'swig'
36 path = req.resource.view?.path or req.resource.shortName or 'fallback.html'
37 cons[engine] path, res.data, (err, html) ->
38 if err? then next err else res.end html
39
40 # Define a custom error type, we must use a class in CoffeeScript to get a
41 # named function.
42 class TeapotError
43 constructor: ->
44
45 # and register it with the app
46 @error TeapotError, (err, req, res, next) ->
47 res.statusCode = 418
48 res.end """
49 I'm a little teapot, short and stout.
50 Here is my handle, here is my spout.
51 When I get all steamed up, hear me shout!
52 Tip! me over and pour me out!
53 """
54
55<<<<<<< HEAD
56 @resource '/teapot': GET: -> @error 'TeapotError'
57=======
58 @resource '/teapot': GET: -> @error new TeapotError
59>>>>>>> b3a39c4... Update example app