Permalink
Browse files

removing `json` method from response and adding `render`, using conso…

…lidate.js ftw
  • Loading branch information...
1 parent 35da65a commit f44f0779e3ea0c396844b61e2a6bd491ce10a41a @mvrilo committed Dec 7, 2011
Showing with 81 additions and 11 deletions.
  1. +36 −0 examples/public/text.jade
  2. +18 −0 examples/render.js
  3. +9 −5 lib/frank.js
  4. +17 −5 lib/response.js
  5. +1 −1 package.json
View
@@ -0,0 +1,36 @@
+| An example of an
+a(href='#') inline
+| link.
+
+form
+ label Username:
+ input(type='text', name='user[name]')
+ p
+ | Just an example of some text usage.
+ | You can have <em>inline</em> html,
+ | as well as
+ strong tags
+ | .
+
+ | Interpolation is also supported. The
+ | username is currently "#{name}".
+
+ label Email:
+ input(type='text', name='user[email]')
+ p
+ | Email is currently
+ em= email
+ | .
+
+ // alternatively, if we plan on having only
+ // text or inline-html, we can use a trailing
+ // "." to let jade know we want to omit pipes
+
+ label Username:
+ input(type='text')
+ p.
+ Just an example, like before
+ however now we can omit those
+ annoying pipes!.
+
+ Wahoo.
View
@@ -0,0 +1,18 @@
+var frank = require('../lib/frank'),
+ server = frank();
+
+server
+ .get('/')
+ .render('jade', './public/text.jade', {
+ name: 'murilo',
+ email: 'mvrilo@gmail.com'
+ });
+
+server
+ .get('/jade')
+ .render('./public/text.jade', { // this also works!
+ name: 'murilo',
+ email: 'mvrilo@gmail.com'
+ });
+
+server.listen(8000)
View
@@ -15,22 +15,22 @@ module.exports = frank;
'get post put head'.split(' ').forEach(function(method) {
frank.fn[method] = function(path) {
this.server = connect();
- this.handler = {};
+ this.handler = this.handler || {};
this.path = path;
this.route = [method.toUpperCase(), path].join(' ');
this.handler[this.route] = {};
- this.handler[this.route]['method'] = method.toUpperCase();
- this.handler[this.route]['path'] = path;
+ this.handler[this.route].method = method.toUpperCase();
+ this.handler[this.route].path = path;
if (!this.scope) this.__proto__ = frank.fn.__proto__;
return this;
};
});
frank.fn.then = function(callback) {
- this.handler[this.route]['callback'] = callback;
+ this.handler[this.route].callback = callback;
return this;
}
@@ -51,6 +51,8 @@ frank.server = function() {
path = handler.path,
method = handler.method,
auth = handler.auth,
+ body = handler.body,
+ contentType = handler.contentType,
callback = handler.callback;
if (auth) {
@@ -65,7 +67,9 @@ frank.server = function() {
else {
server.use(path, function(req, res) {
if (req.method == self.handler[key].method) {
- res.end(self.handler[key].body);
+ res.setHeader('Content-Type', contentType);
+ res.setHeader('Content-Length', body.length);
+ res.end(body);
}
});
}
View
@@ -1,10 +1,22 @@
+var cons = require('consolidate').render;
+
module.exports = {
- json : function(obj) {
- this.handler[this.route]['body'] = JSON.stringify(obj);
+ send : function(str, type) {
+ type = type || 'text/plain';
+ this.handler[this.route].contentType = type;
+ this.handler[this.route].body = str;
return this;
},
- send : function(str) {
- this.handler[this.route]['body'] = str;
- return this;
+ render : function(ext, path, options) {
+ var self = this;
+ if (!options) {
+ options = path;
+ path = ext;
+ ext = path.replace(/^.*\.(.*)$/, "$1");
+ }
+ ext = ext.replace('.', '');
+ cons[ext].call(null, path, options, function(err, str) {
+ if (!err) self.send(str, 'text/html');
+ });
}
};
View
@@ -9,6 +9,6 @@
"url" : "http://github.com/mvrilo/frank.git"
},
"main" : "./lib/frank.js",
- "dependencies": { "connect": "< 2.0.0" },
+ "dependencies": { "connect": "< 2.0.0", "consolidate": "*" },
"engines": { "node": ">= 0.5.0 < 0.7.0" }
}

0 comments on commit f44f077

Please sign in to comment.