Skip to content

Commit ebb8f81

Browse files
committed
Adding router
1 parent 64bf741 commit ebb8f81

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

lib/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var Diamonator = function (server) {
2+
3+
var self = this;
4+
5+
this._routes = {};
6+
server.on('request', function (req, res) {
7+
8+
var route = self._route(req.method, req.path);
9+
if (route) {
10+
return route(req, res);
11+
}
12+
13+
res.writeHead(404);
14+
15+
});
16+
};
17+
18+
Diamonator.prototype.register = function (route, callback) {
19+
20+
this._routes[route.method] = this._routes[route.method] || {};
21+
this._routes[route.method][route.path] = callback;
22+
};
23+
24+
25+
Diamonator.prototype._route = function (method, path) {
26+
27+
return this._routes[method][path];
28+
};
29+
30+
module.exports = Diamonator;

test/helper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ exports.createServer = function () {
1414

1515
var res = new Stream.PassThrough();
1616
res.result = '';
17-
res.on('readable', function (chunk) {
17+
res.on('readable', function () {
1818

19-
res.result += chunk.toString();
19+
res.result += res.read().toString();
2020
});
2121

2222
server.emit('request', req, res);
23-
res.once('finish', callback);
23+
res.once('end', function () {
24+
25+
callback(res);
26+
});
2427
};
2528

2629
return server;

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ describe('register routes', function () {
2020
var diamonator = new Diamonator(server);
2121
diamonator.register({ method: 'GET', path: '/' }, function (req, res) {
2222

23-
res.end('OK');
23+
res.write('OK');
24+
res.end();
2425
});
2526

2627
server.inject('GET', '/', function (res) {
2728

28-
expect(res.statusCode).to.equal(200);
2929
expect(res.result).to.equal('OK');
3030
done();
3131
});

0 commit comments

Comments
 (0)