Skip to content

Commit 701adc4

Browse files
improved README + ready for 2.1.2
1 parent 5c6404a commit 701adc4

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

README.md

+58-45
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,61 @@ Classes http.ServerRequest and http.ServerResponse earns new params property con
66
Using httpdispatcher is pretty simple:
77

88
```js
9-
var HttpDispatcher = require('../httpdispatcher');
10-
var http = require('http');
11-
var dispatcher = new HttpDispatcher();
12-
13-
dispatcher.setStatic('/resources');
14-
dispatcher.setStaticDirname('static');
15-
16-
dispatcher.onGet("/page1", function(req, res) {
17-
res.writeHead(200, {'Content-Type': 'text/plain'});
18-
res.end('Page One');
19-
});
20-
21-
dispatcher.onPost("/page2", function(req, res) {
22-
res.writeHead(200, {'Content-Type': 'text/plain'});
23-
res.end('Page Two');
24-
});
25-
26-
dispatcher.beforeFilter(/\//, function(req, res, chain) { //any url
27-
console.log("Before filter");
28-
chain.next(req, res, chain);
29-
});
30-
31-
dispatcher.afterFilter(/\//, function(req, res, chain) { //any url
32-
console.log("After filter");
33-
chain.next(req, res, chain);
34-
});
35-
36-
dispatcher.onError(function(req, res) {
37-
res.writeHead(404);
38-
res.end();
39-
});
40-
41-
http.createServer(function (req, res) {
42-
dispatcher.dispatch(req, res);
43-
}).listen(1337, '127.0.0.1');
44-
45-
46-
/*
47-
GET /page1 => 'Page One'
48-
POST /page2 => 'Page Two'
49-
GET /page3 => 404
50-
GET /resources/images-that-exists.png => Image resource
51-
GET /resources/images-that-does-not-exists.png => 404
52-
*/
53-
```
9+
var HttpDispatcher = require('../httpdispatcher');
10+
var http = require('http');
11+
var dispatcher = new HttpDispatcher();
12+
13+
dispatcher.setStatic('/resources');
14+
dispatcher.setStaticDirname('static');
15+
16+
dispatcher.onGet("/page1", function(req, res) {
17+
res.writeHead(200, {'Content-Type': 'text/plain'});
18+
res.end('Page One');
19+
});
20+
21+
dispatcher.onPost("/page2", function(req, res) {
22+
res.writeHead(200, {'Content-Type': 'text/plain'});
23+
res.end('Page Two');
24+
});
25+
26+
dispatcher.beforeFilter(/\//, function(req, res, chain) { //any url
27+
console.log("Before filter");
28+
chain.next(req, res, chain);
29+
});
30+
31+
dispatcher.afterFilter(/\//, function(req, res, chain) { //any url
32+
console.log("After filter");
33+
chain.next(req, res, chain);
34+
});
35+
36+
dispatcher.onError(function(req, res) {
37+
res.writeHead(404);
38+
res.end();
39+
});
40+
41+
http.createServer(function (req, res) {
42+
dispatcher.dispatch(req, res);
43+
}).listen(1337, '127.0.0.1');
44+
45+
46+
/*
47+
GET /page1 => 'Page One'
48+
POST /page2 => 'Page Two'
49+
GET /page3 => 404
50+
GET /resources/images-that-exists.png => Image resource
51+
GET /resources/images-that-does-not-exists.png => 404
52+
*/
53+
```
54+
55+
request and response
56+
---------
57+
58+
Every listeners is called with two parameters `request` and `response`.
59+
60+
Request object is an instance of [`http.ClientRequest`](https://nodejs.org/api/http.html#http_class_http_clientrequest) with some custom properties:
61+
62+
- bodyBuffer : [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer) (available only on POST request)
63+
- body : String (available only on POST request)
64+
- params : Object
65+
66+
Response object is an instance of [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse).

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"main": "httpdispatcher.js",
1414
"name": "httpdispatcher",
1515
"repository": {"url": "https://github.com/alberto-bottarini/httpdispatcher", "type": "git"},
16-
"version": "2.1.1",
16+
"version": "2.1.2",
1717
"license": "MIT"
1818
}

0 commit comments

Comments
 (0)