1 | This is a collection of useful middlewares.
|
2 |
|
3 | # redirect
|
4 |
|
5 | node middleware that handles redirection.
|
6 |
|
7 | ## Usage
|
8 |
|
9 | ```coffeescript
|
10 | {redirect} = require 'middleware'
|
11 |
|
12 | # redirects `/foo` to `/bar`
|
13 | router.get '/foo',
|
14 | redirect '/bar'
|
15 |
|
16 | # redirects `/foo/$var` to `/bar/$var`
|
17 | router.get '/foo/:id',
|
18 | redirect (req) -> "/bar/#{req.params.id}"
|
19 | ```
|
20 |
|
21 |
|
22 | # render
|
23 |
|
24 | middleware to render arbitrary views.
|
25 | This middleware can be used with all existing template engines.
|
26 |
|
27 | ## Usage
|
28 |
|
29 | Middleware called before `render` should populate the `req.loaded` object
|
30 | with data that the view should present to the client
|
31 | `render` will send the output of `view req.loaded` to the client
|
32 |
|
33 | ## Example
|
34 |
|
35 | ```coffeescript
|
36 | jsonView = (params) -> JSON.stringify params
|
37 |
|
38 | router.get '/resource',
|
39 | (req, res, next) ->
|
40 | req.loaded =
|
41 | foo: 'bar'
|
42 | fitz: [1..10]
|
43 | next()
|
44 | render jsonView
|
45 |
|
46 | # will render `'{"foo":"bar","fitz":[1,2,3,4,5,6,7,8,9,10]}'` to the
|
47 | # client
|
48 | ```
|
49 |
|
50 | # bind
|
51 |
|
52 | Transforms an asynchronous function into a middleware.
|
53 | The result of the function will be bound to `request.loaded`.
|
54 |
|
55 | Use this library if you want to load async data in a clean
|
56 | way.
|
57 |
|
58 | ## Short Description
|
59 |
|
60 | Transforms an asynchronous function `f` into a middleware.
|
61 | The middleware will call `f` with `(req, callback)`.
|
62 | `f` has to call `callback` with `err, value`
|
63 | which will cause `value` to be bound to `req.loaded[name]`.
|
64 |
|
65 | ## Usage
|
66 |
|
67 | ```coffeescript
|
68 | bind name, (req, next) ->
|
69 | # do async stuff and call `next` when you are finished
|
70 | error = null
|
71 | result = 'result'
|
72 | next error, result
|
73 | ```
|
74 |
|
75 | `name` is the directive where to save the results from `function`.
|
76 |
|
77 | ```coffeescript
|
78 | # will bind the result of asnycMethod to `req.loaded.foo`
|
79 | bind 'foo', (req, next) -> asyncMethod next
|
80 | ```
|
81 |
|
82 | `name` can be nested.
|
83 |
|
84 | ```coffeescript
|
85 | # will bind the result of asyncMethod to `req.loaded.foo.bar`
|
86 | bind 'foo.bar', (req, next) -> asyncMethod next
|
87 | ```
|
88 |
|
89 | If the asynchronous method returns an error the process is aborted
|
90 | and a Status Code of 500 is returned to the user.
|
91 |
|
92 | ## Example
|
93 |
|
94 | ```coffeescript
|
95 | router.get '/user',
|
96 | bind 'users.all', (req, next) -> dao.user.all next
|
97 | (req, res, next) -> next console.log 'loaded users', req.loaded.users.all
|
98 | # will output the result of `dao.user.all`
|
99 | render view.user.all
|
100 |
|
101 | router.get '/user/:id',
|
102 | bind 'user.object', (req, next) -> dao.user.byId req.params.id, next
|
103 | render view.user.show
|
104 | ```
|
105 |
|
106 |
|
107 | # exec
|
108 |
|
109 | Simple way to create middleware that should not modify the `response`.
|
110 | Takes care of errors.
|
111 | Useful for tasks like updating the database.
|
112 |
|
113 | ## Example
|
114 |
|
115 | ```coffeescript
|
116 | router.post '/user/:id',
|
117 | exec (req, next) -> dao.user.update req.params.id, req.body, next
|
118 | ```
|
119 |
|
120 | ## Without `exec`
|
121 |
|
122 | ```coffeescript
|
123 | router.post '/user/:id',
|
124 | dao.user.update req.params.id, req.body, (err) ->
|
125 | if err?
|
126 | res.writeHead 500
|
127 | res.end 'Internal Server Error'
|
128 | else
|
129 | next()
|
130 | ```
|
131 |
|
132 |
|
133 | # flash
|
134 |
|
135 | used for setting, loading flash messages.
|
136 |
|
137 | ```coffeescript
|
138 |
|
139 | {loadFlash, flash, redirect, render} = require 'mw'
|
140 |
|
141 | router.post '/foo',
|
142 | flash 'hello flash'
|
143 | redirect 'back'
|
144 |
|
145 | router.get '/foo'
|
146 | loadFlash
|
147 | (req, res, next) -> next console.log req.loaded.flash
|
148 | # prints {message: 'hello flash', type: 'info'
|
149 | # view.foo will receive `req.loaded` and can print them to the dom
|
150 | render view.foo
|
151 |
|
152 | ```
|
153 |
|
154 | A view helper for displaying alerts can be found in
|
155 | [dombox](https://github.com/mren/dombox).
|
156 |
|
157 | # condition
|
158 |
|
159 | `if-else` statement as a middleware
|
160 |
|
161 | ```coffeescript
|
162 |
|
163 | {condition} = require 'mw'
|
164 |
|
165 | # the condition is dependant on the state
|
166 | # in this example we use this simplified conditions
|
167 | alwaysTrue = (req) -> true
|
168 | alwaysFalse = (req) -> false
|
169 |
|
170 | # this would be a useful condition
|
171 | isLoggedIn = (req) -> req.session?.user?
|
172 |
|
173 | log = (msg) -> (req, res, next) -> next console.log msg
|
174 |
|
175 |
|
176 | # for details about sequenz see https://github.com/snd/sequenz
|
177 | http.createServer sequenz [
|
178 | condition alwaysTrue log('this middleware is executed'), log('this middleware is ignored')
|
179 | condition alwaysFalse log('this middleware is ignored'), log('this middleware is executed')
|
180 | condition alwaysFalse log('nothing is shown and the next middleware is executed')
|
181 | (req, res, next) -> req.end()
|
182 | ]
|
183 | ```
|