Permalink
Browse files

Manage HTTP parameters

  • Loading branch information...
alberto-bottarini committed May 21, 2012
1 parent 74ba53b commit 047d244106c6df33fa372d28ebe462cab43852a9
Showing with 34 additions and 11 deletions.
  1. +1 −0 README
  2. +32 −10 httpdispatcher.js
  3. +1 −1 package.json
View
1 README
@@ -2,6 +2,7 @@ httpdispatcher - basic dispatcher for node.js
=======
httpdispatcher is a simple class allows developer to have a clear dispatcher for dynamic pages and static resources.
+Classes http.ServerRequest and http.ServerResponse earns new params property containing a map of received HTTP parameters.
Using httpdispatcher is pretty simple:
var dispatcher = require('./httpdispatcher');
View
@@ -2,7 +2,10 @@ var util = require('util');
var HttpDispatcher = function() {
this.listeners = { get: [ ], post: [ ] };
this.filters = { before: [ ], after: [ ] };
- this.errorListener = function() { }
+ this.errorListener = function(req, res) {
+ res.writeHead(404);
+ res.end();
+ }
this.staticFolderPrefix = '/static';
}
HttpDispatcher.prototype.on = function(method, url, cb) {
@@ -38,15 +41,34 @@ HttpDispatcher.prototype.afterFilter = function(url, cb) {
HttpDispatcher.prototype.dispatch = function(req, res) {
var url = require('url').parse(req.url, true);
var method = req.method.toLowerCase();
- var httpChain = new HttpChain();
- var beforeFilters = this.getFilters(url.pathname, 'before');
- httpChain.addAll(beforeFilters);
- var listener = this.getListener(url.pathname, method);
- var listenerCb = listener ? listener : this.errorListener;
- httpChain.add(httpChain.getWrapped(listenerCb));
- var afterFilters = this.getFilters(url.pathname, 'after');
- httpChain.addAll(afterFilters);
- httpChain.next(req, res);
+ var dispatcher = this;
+ var doDispatch = function() {
+ var httpChain = new HttpChain();
+ var beforeFilters = this.getFilters(url.pathname, 'before');
+ httpChain.addAll(beforeFilters);
+ var listener = this.getListener(url.pathname, method);
+ var listenerCb = listener ? listener : this.errorListener;
+ httpChain.add(httpChain.getWrapped(listenerCb));
+ var afterFilters = this.getFilters(url.pathname, 'after');
+ httpChain.addAll(afterFilters);
+ httpChain.next(req, res);
+ }
+ if(method == 'post') {
+ var body = '';
+ req.on('data', function(data) {
+ body += data;
+ });
+ req.on('end', function() {
+ var post = require('querystring').parse(body);
+ req.body = body;
+ req.params = post;
+ doDispatch.call(dispatcher);
+ });
+ } else {
+ var url_parts = require('url').parse(req.url, true);
+ req.params = url_parts.query;
+ doDispatch.call(dispatcher);
+ }
}
HttpDispatcher.prototype.staticListener = function(req, res) {
var url = require('url').parse(req.url, true);
View
@@ -13,5 +13,5 @@
"main": "httpdispatcher.js",
"name": "httpdispatcher",
"repository": {"url": "https://github.com/alberto-bottarini/httpdispatcher", "type": "git"},
- "version": "0.2.1"
+ "version": "0.3.0"
}

0 comments on commit 047d244

Please sign in to comment.