Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Sep 1, 2012
0 parents commit 2f70b7f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Empty file added README.md
Empty file.
49 changes: 49 additions & 0 deletions 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 changes: 11 additions & 0 deletions 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.