Thursday 15 April 2010

node.js - http request does not emit 'end' after proxy -



node.js - http request does not emit 'end' after proxy -

i need able utilize http request body in request proxy application , 1 time again in actual web service. using restreamer 'reset' stream (and wrote middleware myself no change). web service receives body fine, because end never emitted, cannot go on request.

testing postman, sending raw body, content type set. suggestions appreciated. thanks!

var express = require('express') , bodyparser = require('body-parser') , http = require('http') , restreamer = require('connect-restreamer') , httpproxy = require('http-proxy') var app = express(); app.use(function (req, res, next) { var body = ''; req.on('data', function (chunk) { body += chunk.tostring('utf8'); }); req.on('end', function (chunk) { req.body = json.parse(body) next(); }); }); app.use(restreamer()); var proxy = httpproxy.createserver(); app.all('*', function (req, res) { proxy.web(req, res, { target: 'http://localhost:8001' }); }); http.createserver(app).listen(8000); app2 = express(); app2.use(function (req, res, next) { var body = ''; req.on('data', function (chunk) { body += chunk.tostring('utf8'); }); req.on('end', function (chunk) { req.body = json.parse(body) next(); }); }); app2.all('*', function (req, res) { res.send(req.body) }); http.createserver(app2).listen(8001);

using request library in application, worked:

var request = require('request') request.post({ url: 'http://localhost:8000', json: {content: 123, type: "greeting", access_token: "here am"} },function(err, res,data){ console.log('return:' ,err, data) });

but using curl file containing same message, not work:

curl -x post -d @data.json http://localhost:8000 --header "content-type:application/json"

i compared request objects against each other , found few differences , when got request header content-length, found editing "correct" length end steam (and web server send response).

i create modifications needed , commit connect-restreamer module.

node.js express node-http-proxy

No comments:

Post a Comment