如何在客户端javascript中使用node.js缓冲库 [英] How can I use node.js buffer library in client side javascript
问题描述
我想在我的网站中使用缓冲库(以处理二进制数据).这是我的用例:
I would like to use buffer library (in order handle binary data) in my website. here is my use case:
const privateKey = Buffer.from('<User's private key here>', 'hex');
buffer在node.js中可以正常工作,而无需任何其他npm模块或脚本.但不知何故,它无法在网络浏览器中工作.它显示了一个错误
buffer works fine in node.js without any additional npm module or script. but somehow,it is not working in web browser.it is showing an error
未捕获的引用错误:未定义缓冲区
uncaught refernce error: buffer is not defined
尽管我需要在HTML文件中添加库脚本文件.请帮我解决这个问题?
I though we need to add library script file in our html file. please help me to fix this?
推荐答案
缓冲对象在Node.js外部(即在浏览器中)不可用.这是因为(如果您不知道)Node.js是javascript运行时,因此Node.js特定功能在浏览器环境中不存在,因为它们与V8引擎关联,但与浏览器中的V8引擎无关.(请注意此处的区别).
The buffer object is not available outside of Node.js, i.e in the browser. This is because (in case you were not aware) Node.js is a javascript runtime, therefore Node.js specific functionalities do not exist within a browser environment, because they are associated with the V8 engine, but not the V8 engine within a browser (note the difference here).
因此,实质上,未捕获引用错误:未定义缓冲区
表示此浏览器中不存在此错误.
So essentially, uncaught refernce error: buffer is not defined
means that this this doesn't exist in the browser.
https://nodejs.org/api/buffer.html#buffer_new_buffer_array
这篇关于如何在客户端javascript中使用node.js缓冲库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!