UNPKG

4.25 kBMarkdownView Raw
1This is a collection of useful middlewares.
2
3# redirect
4
5node middleware that handles redirection.
6
7## Usage
8
9```coffeescript
10{redirect} = require 'middleware'
11
12# redirects `/foo` to `/bar`
13router.get '/foo',
14 redirect '/bar'
15
16# redirects `/foo/$var` to `/bar/$var`
17router.get '/foo/:id',
18 redirect (req) -> "/bar/#{req.params.id}"
19```
20
21
22# render
23
24middleware to render arbitrary views.
25This middleware can be used with all existing template engines.
26
27## Usage
28
29Middleware called before `render` should populate the `req.loaded` object
30with 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
36jsonView = (params) -> JSON.stringify params
37
38router.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
52Transforms an asynchronous function into a middleware.
53The result of the function will be bound to `request.loaded`.
54
55Use this library if you want to load async data in a clean
56way.
57
58## Short Description
59
60Transforms an asynchronous function `f` into a middleware.
61The middleware will call `f` with `(req, callback)`.
62`f` has to call `callback` with `err, value`
63which will cause `value` to be bound to `req.loaded[name]`.
64
65## Usage
66
67```coffeescript
68bind 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`
79bind '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`
86bind 'foo.bar', (req, next) -> asyncMethod next
87```
88
89If the asynchronous method returns an error the process is aborted
90and a Status Code of 500 is returned to the user.
91
92## Example
93
94```coffeescript
95router.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
101router.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
109Simple way to create middleware that should not modify the `response`.
110Takes care of errors.
111Useful for tasks like updating the database.
112
113## Example
114
115```coffeescript
116router.post '/user/:id',
117 exec (req, next) -> dao.user.update req.params.id, req.body, next
118```
119
120## Without `exec`
121
122```coffeescript
123router.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
135used for setting, loading flash messages.
136
137```coffeescript
138
139{loadFlash, flash, redirect, render} = require 'mw'
140
141router.post '/foo',
142 flash 'hello flash'
143 redirect 'back'
144
145router.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
154A 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
167alwaysTrue = (req) -> true
168alwaysFalse = (req) -> false
169
170# this would be a useful condition
171isLoggedIn = (req) -> req.session?.user?
172
173log = (msg) -> (req, res, next) -> next console.log msg
174
175
176# for details about sequenz see https://github.com/snd/sequenz
177http.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```