Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 2f70b7f47d0737c5768848a2da022a4260657948 0 parents
@pgte authored
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"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.