4

I would like to send a Buffer through Http response, but on the client I receive it as string instead of Buffer. I use expressjs router, as below code

router.get('/', function(req, res, next) {
  const buf = new Buffer('Hello world');
  console.log(buf); // gives me <Buffer 48 65 6c 6c 6f 20 77 6f 72 6c 64>
  res.send(buf); // gives me 'Hello world' 
});

In the above code my expected result was Buffer () but I get the String output 'Hello World.

Someone help me out. Thanks in advance.

3
  • 1
    Client don't know nothing about NodeJS buffer, so it can't use it. Commented Apr 12, 2016 at 11:35
  • What exactly did you expect to receive? The hexadecimal representation of the buffer contents? Commented Apr 12, 2016 at 12:48
  • 1
    Buffer content something like this <Buffer 48 65 6c 6c 6f 20 77 6f 72 6c 64> Commented Apr 13, 2016 at 6:20

1 Answer 1

5

As mentioned in Express doc, http://expressjs.com/en/api.html

When the parameter is a Buffer object, the method sets the Content-Type response header field to “application/octet-stream”

Depends on your browser, some may download the response as a file (such as Chrome), some may read the stream and display the content directly (such as IE11).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.