github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/test/test_apps/test_nodejs_app/node_modules/httpdispatcher/README.md (about)

     1  httpdispatcher - basic dispatcher for node.js
     2  =======
     3  
     4  httpdispatcher is a simple class allows developer to have a clear dispatcher for dynamic pages and static resources.
     5  Classes http.ServerRequest and http.ServerResponse earns new params property containing a map of received HTTP parameters.
     6  Using httpdispatcher is pretty simple:
     7  
     8  ```js
     9  	var dispatcher = require('./httpdispatcher');
    10  
    11  	dispatcher.setStatic('resources');
    12  	dispatcher.setStaticDirname('.');
    13  	
    14  	dispatcher.onGet("/page1", function(req, res) {
    15  		res.writeHead(200, {'Content-Type': 'text/plain'});
    16  		res.end('Page One');
    17  	});	
    18  	
    19  	dispatcher.onPost("/page2", function(req, res) {
    20  		res.writeHead(200, {'Content-Type': 'text/plain'});
    21  		res.end('Page Two');
    22  	});
    23  	
    24  	dispatcher.beforeFilter(/\//, function(req, res, chain) { //any url
    25  		console.log("Before filter");
    26  		chain.next(req, res, chain);
    27  	});
    28  	
    29  	dispatcher.afterFilter(/\//, function(req, res) { //any url
    30  		console.log("After filter");
    31  		chain.next(req, res, chain);
    32  	});
    33  	
    34  	dispatcher.onError(function(req, res) {
    35  		res.writeHead(404);
    36  	});
    37  	
    38  	http.createServer(function (req, res) {
    39  		dispatcher.dispatch(req, res);
    40  	}).listen(1337, '127.0.0.1');
    41  	
    42  	
    43  	/*
    44  	GET /page1 => 'Page One'
    45  	POST /page2 => 'Page Two'
    46  	GET /page3 => 404
    47  	GET /resources/images-that-exists.png => Image resource
    48  	GET /resources/images-that-does-not-exists.png => 404
    49  	*/
    50  ```