@@ -6,48 +6,61 @@ Classes http.ServerRequest and http.ServerResponse earns new params property con
6
6
Using httpdispatcher is pretty simple:
7
7
8
8
``` 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 ) .
0 commit comments