Shankar Sengalani
Shankar Sengalani

Reputation: 302

Nodejs + expressjs get http response as Buffer

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.

Upvotes: 4

Views: 11748

Answers (1)

Anthony C
Anthony C

Reputation: 2157

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).

Upvotes: 5

Related Questions