Browse files

first commit

  • Loading branch information...
0 parents commit 2f70b7f47d0737c5768848a2da022a4260657948 @pgte committed Sep 1, 2012
Showing with 60 additions and 0 deletions.
  1. 0 README.md
  2. +49 −0 index.js
  3. +11 −0 package.json
0 README.md
No changes.
49 index.js
@@ -0,0 +1,49 @@
+var qs = require('querystring');
+
+var parsers = {
+ 'application/x-www-form-urlencoded': qs.parse,
+ 'application/json': JSON.parse
+};
+
+function mime(req) {
+ var str = req.headers['content-type'] || '';
+ return str.split(';')[0];
+}
+
+function parse(req, body) {
+
+ var parser = parsers[mime(req)];
+
+ if (parser) {
+
+ body = parser(body);
+ return body;
+ }
+}
+
+
+function MethodOverride(){
+ return function methodOverride(req, res) {
+ if (req.method === 'POST') {
+
+ var body = '';
+ req.setEncoding('utf8');
+
+ req.on('data', function(d) {
+ body += d;
+ });
+
+ req.on('end', function() {
+ body = parse(req, body);
+
+ if (body._method) {
+ req.method = body._method;
+ }
+ });
+
+ }
+ res.emit('next');
+ };
+}
+
+module.exports = MethodOverride;
11 package.json
@@ -0,0 +1,11 @@
+{
+ "name": "flatware-method-override",
+ "version": "0.1.0",
+ "author": {
+ "name": "Pedro Teixeira",
+ "email": "pedro.teixeira@gmail.com"
+ },
+ "engines": {
+ "node": ">= 0.8.0"
+ }
+}

0 comments on commit 2f70b7f

Please sign in to comment.