Permalink
Browse files

Re-implemented connect's bodyParser - it stopped working for me

I can't explain why.  git bisect showed that it doesn't work in
source history either, which contradicts my memory of successfully
running make in prior commits.  That points to some environmental
change leading to a non-deterministic build, but I couldn't figure
out what that environmental issue is.
  • Loading branch information...
1 parent f59370f commit 95a51519d43c9490c0ed3855c8bbbd667bd7a3cb @bbyars committed May 29, 2011
Showing with 22 additions and 1 deletion.
  1. +1 −0 TODO.md
  2. +21 −1 server/lib/controlServer.js
View
@@ -1,6 +1,7 @@
## Refactorings
* filter out stub matching logic into module, unit test
* Remove string.format for something more javascript like
+* Use connect's bodyParser rather than re-implementing
## Build
* packages: npm, brew
@@ -18,6 +18,26 @@ var create = function (port) {
contentHeader = {'Content-Type': CONTENT_TYPE},
app;
+ // express.bodyParser() stopped working for me.
+ // git bisect showed that it doesn't work in source history either,
+ // pointing to environmental issues, but I couldn't figure out what
+ // changed (same version of node, express, connect, etc)...
+ function bodyParser(request, response, next) {
+ request.rawBody = '';
+ request.setEncoding('utf8');
+
+ request.on('data', function (chunk) {
+ request.rawBody += chunk;
+ });
+
+ request.on('end', function () {
+ if (request.rawBody !== '') {
+ request.body = JSON.parse(request.rawBody);
+ }
+ next();
+ });
+ }
+
function createAbsoluteUrl(request, response, next) {
var host = request.headers.host || 'localhost:' + port;
response.absoluteUrl = function (endpoint, serverPort) {
@@ -88,7 +108,7 @@ var create = function (port) {
app = express.createServer(
express.logger({format: '[ROOT]: :method :url'}),
- express.bodyParser(),
+ bodyParser,
createAbsoluteUrl);
app.listen(port);
console.log('HTTPMock running at http://localhost:{0}'.format(port));

0 comments on commit 95a5151

Please sign in to comment.