If you are actually looking to buffer the full request you can do something
like following:
function callServer(req, res) {
var body = '' ;
var options = { host: 'localhost', port: 3000, path: req.url } ;
var callback = function(response) {
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
});
response.on('end', function () {
res.writeHead(response.statusCode,
response.headers) ;
res.end(Buffer.concat(chunks)) ;
});
} ;
http.request(options, callback).end();
}
In your example it is the += that is converting to a string that messes up
the result. Some buffer bytes get expanded out to multiple utf bytes when
you do that.
On Fri, Nov 9, 2012 at 3:25 PM, Jeanluca <[email protected]> wrote:
> Hi
>
> I'm trying to cache the responses of a server. But before I can do this I
> need the following to work
>
> var http = require('http'),
>
> http.createServer(function (req, res) {
> callServer(req, res);
> }).listen(8000);
>
> function callServer(req, res) {
> var body = '' ;
> var options = { host: 'localhost', port: 8080, path: req.url } ;
>
> var callback = function(response) {
> response.on('data', function (chunk) {
> body += chunk;
> });
>
> response.on('end', function () {
> res.writeHead(response.statusCode,
> response.headers) ;
> res.end(body) ;
> });
> } ;
> http.request(options, callback).end();
> }
>
> This works very well for html and css, but not for images. I tried
> "res.end(body, 'binary')" without success. Furthermore, I noticed that
> body.length != response.headers['content-length']
> Any suggestion how to fix this ?
>
> Cheer
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en