4

How can i use/call Nodejs's Buffer in Angularjs ? i spent many hours in google but i can not find the answer. Thanks for your help.

V-Q-A NGUYEN
  • 1,497
  • 16
  • 19
  • In nodeJs, i wrote some code like: var buffer = new Buffer(9); buffer.writeUInt8(3, 0); buffer.writeUInt32LE(0x00040000, 1); buffer.writeUInt32LE(10, 5); how i can do the same thing in Angulars JS ? Thanks for any reply. – V-Q-A NGUYEN May 26 '17 at 15:00
  • 1
    Buffer is a native Node.js API. It does not exist in standard JS – borislemke May 26 '17 at 15:02
  • @borislemke: Thanks for your reply. i found this module https://www.npmjs.com/package/buffer, can i import and use it in my Angular app ? if yes, how can i do it ? – V-Q-A NGUYEN May 26 '17 at 15:17
  • possible duplicate of https://stackoverflow.com/questions/8880571/convert-nodejs-buffer-to-browsers-javascript – V-Q-A NGUYEN May 26 '17 at 15:23
  • I'm not familiar with that module, but if it works as it describes itself, you might as well give it a try – borislemke May 26 '17 at 15:32
  • i'm always searching the solution. i appreciate any help that you can provide. – V-Q-A NGUYEN Jun 23 '17 at 09:07

1 Answers1

3

I found the solution. The idea is using ArrayBuffer, DataView of Javascript to make the same thing as Buffer of NodeJS.

For example, in NodeJS, i wrote something:

var buffer = new Buffer(9); 
buffer.writeUInt8(3, 0); 
buffer.writeUInt32LE(0x00040000, 1); 

The corresponding code in Javascript like the following:

 var buffer = new ArrayBuffer(9);
 var dataview = new DataView(buffer);
 dataview.setUint8(0, 3);            
 dataview.setUint32(1, parseInt('0x00040000'), true);

Hope this helps for anyone have the same problem.

V-Q-A NGUYEN
  • 1,497
  • 16
  • 19
  • Hi [@V-Q-A-NGUYEN](https://stackoverflow.com/users/2599865/v-q-a-nguyen) , do you know how can I use this method to convert this type of buffer usage?`Buffer.from('some text', 'utf8')` – Leonardo Rick Aug 19 '20 at 00:50