Created
March 20, 2017 12:08
-
-
Save Chunlin-Li/1f013483dff79f630cf3c92d9c2b1277 to your computer and use it in GitHub Desktop.
co-body v5.0.3 reproduce
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
'use strict'; | |
var koa = require('koa'); | |
var bodyParse = require('koa-bodyparser'); | |
var app = new koa(); | |
app.use(function *(next) { | |
let bodyData = []; | |
this.req.on('data', (chunk) => { | |
bodyData.push(chunk); | |
}); | |
this.req.on('end', () => { | |
this.req.rawBody = Buffer.concat(bodyData).toString(); | |
}); | |
return yield next; | |
}); | |
app.use(bodyParse()); | |
app.listen(2000, function() { | |
let data = JSON.stringify({foo:'bar'}); | |
var request = require('http').request({ | |
hostname: '127.0.0.1', | |
port: 2000, | |
method: 'POST', | |
path: '/foo', | |
headers: { | |
'Content-Type' : 'application/json', | |
} | |
}, function (resp) { | |
resp.pipe(process.stdout); | |
}); | |
request.end(data); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment