This repository has been archived by the owner on Mar 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frank.js
65 lines (56 loc) · 1.63 KB
/
frank.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var connect = require('connect'),
request = require('./request'),
response = require('./response'),
merge = connect.utils.merge;
function frank() {
if (!(this instanceof frank)) return new frank();
this.server = connect.createServer();
this.handler = {};
}
merge(request, response);
frank.fn = frank.prototype;
'get post put head'.split(' ').forEach(function(method) {
frank.fn[method] = function(path) {
if (!this.handler) {
this.server = connect();
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.__proto__ = request;
return this;
};
});
frank.fn.then = function(callback) {
this.handler[this.route]['callback'] = callback;
return this;
}
frank.fn.listen = function(port, msg) {
this.port = port;
msg = msg || "Frank is smoking at " + port;
frank.server.call(this).listen(port);
console.log(msg);
};
frank.fn.run = frank.fn.listen;
frank.server = function() {
var self = this, server = this.server;
server.use(connect.favicon());
for (var key in this.handler) {
if (this.handler[key].auth) server.use(this.handler[key].basicAuth);
if (this.handler[key].callback) {
server.use(this.handler[key].path, this.handler[key].callback);
}
else {
server.use(this.handler[key].path, function(req, res) {
if (req.method == self.handler[key].method) {
res.end(self.handler[key].body);
}
});
}
}
return server;
};
module.exports = frank;