Permalink
Browse files

second argument as callback for method request; better management of …

…plugins; fix the loop for routes with Object.keys/forEach; add example for callback in req method
  • Loading branch information...
1 parent f44f077 commit 28398bb9af15472a80aeaa763b71d46483ec7c00 @mvrilo committed Dec 10, 2011
Showing with 40 additions and 23 deletions.
  1. +2 −4 examples/basic-auth.js
  2. +8 −0 examples/callback.js
  3. +20 −16 lib/frank.js
  4. +10 −3 lib/request.js
View
@@ -1,9 +1,7 @@
var frank = require('../lib/frank'),
server = frank();
-server
- .get('/')
- .basicAuth('admin', 'admin')
- .send('not so minimal api ftw too');
+server.get('/admin').basicAuth('admin', 'admin').send('auth fine');
+server.get('/test').basicAuth('test', 'test').send('ok')
server.listen(8000)
View
@@ -0,0 +1,8 @@
+var frank = require('../lib/frank'),
+ server = frank();
+
+server.get('/', function(req, res) {
+ res.end('as a callback');
+});
+
+server.listen(8000)
View
@@ -10,19 +10,22 @@ function frank() {
frank.fn = frank.prototype;
frank.fn.__proto__ = merge(request, response);
-module.exports = frank;
+module.exports = exports.createServer = frank;
'get post put head'.split(' ').forEach(function(method) {
- frank.fn[method] = function(path) {
- this.server = connect();
- this.handler = this.handler || {};
+ frank.fn[method] = function(path, fn) {
+ this.server = this.server || connect();
+ this.handler = handler = this.handler || {};
this.path = path;
- this.route = [method.toUpperCase(), path].join(' ');
+ this.route = route = [method.toUpperCase(), path].join(' ');
- this.handler[this.route] = {};
- this.handler[this.route].method = method.toUpperCase();
- this.handler[this.route].path = path;
+ handler[route] = {};
+ handler[route].method = method.toUpperCase();
+ handler[route].middleware = [];
+ handler[route].path = path;
+
+ if (fn) this.then(fn);
if (!this.scope) this.__proto__ = frank.fn.__proto__;
return this;
@@ -46,33 +49,34 @@ frank.server = function() {
var self = this, server = this.server;
server.use(connect.favicon());
- for (var key in this.handler) {
+ Object.keys(this.handler).forEach(function(key) {
var handler = this.handler[key],
path = handler.path,
method = handler.method,
- auth = handler.auth,
+ middleware = handler.middleware,
body = handler.body,
contentType = handler.contentType,
callback = handler.callback;
- if (auth) {
- server.use(path, connect.basicAuth(function(user, pwd) {
- return auth[0] === user && auth[1] === pwd;
- }));
+ if (middleware.length) {
+ middleware.forEach(function(plugin, i) {
+ server.use(path, plugin);
+ });
}
if (callback) {
server.use(path, callback);
}
else {
server.use(path, function(req, res) {
- if (req.method == self.handler[key].method) {
+ if (req.method == method) {
res.setHeader('Content-Type', contentType);
res.setHeader('Content-Length', body.length);
res.end(body);
}
});
}
- }
+ });
+
return server;
};
View
@@ -1,11 +1,18 @@
+var connect = require('connect');
+
module.exports = {
setHeader : function(field, value) {
- this.handler[this.route]['header'] = {};
- this.handler[this.route]['header'][field] = value;
+ var route = this.handler[this.route];
+ route.header = {};
+ route.header[field] = value;
return this;
},
basicAuth : function(user, pwd) {
- this.handler[this.route]['auth'] = [user, pwd];
+ this.handler[this.route].middleware.push(
+ connect.basicAuth(function(u, p) {
+ return u === user && p === pwd;
+ })
+ );
return this;
}
};

0 comments on commit 28398bb

Please sign in to comment.