-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f70b7f
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |