简体   繁体   English

什么是node.js绑定?

[英]What are node.js bindings?

I am very new to node.js and I can not seem to find a definition anywhere as to what node.js bindings are. 我是node.js的新手,我似乎无法找到任何关于node.js绑定的定义。 I have seen this term used in slides and nodejs talks but it was never clearly explained. 我已经看到这个术语用于幻灯片和nodejs会谈,但从未明确解释过。 Can anyone help clarify this concept for me? 任何人都可以帮我澄清一下这个概念吗? I have attached a picture of what I am referring to. 我附上了我所指的内容。 在此输入图像描述

Rather than understanding what node.js bindings are, it is more useful to understand what "bindings" are in the first place. 不是理解node.js绑定是什么,而是首先理解“绑定”是什么更有用。

Let's say you are writing a web application where a node.js (JavaScript) backend: 假设您正在编写一个web.apps(JavaScript)后端的Web应用程序:

  1. receives requests from clients, 接收客户的请求,
  2. conducts queries to databases, 对数据库进行查询,
  3. sorts the query results and finally 最后对查询结果进行排序
  4. returns the results to the client. 将结果返回给客户端。

Now normally you would write all the code yourself. 现在通常你会自己编写所有代码。 However, you know that there is an excellent sorting library that can take care of step 3 (ie sorting query results). 但是,您知道有一个很好的排序库可以处理第3步(即排序查询结果)。 The only problem is that the library is written in a system programming language such as C/C++ whereas your code is written in JavaScript. 唯一的问题是库是用C / C ++等系统编程语言编写的,而代码是用JavaScript编写的。 Normally you can't use that library in your code because they are in different programming languages, but with bindings, you can. 通常,您不能在代码中使用该库,因为它们使用不同的编程语言,但是使用绑定,您可以。

Bindings basically are libraries that "bind" two different programming languages so that code written in one language can be used in code written in another library. 绑定基本上是“绑定”两种不同编程语言的库,以便用一种语言编写的代码可以用在另一个库中编写的代码中。 With the presence of bindings, you don't have to write all the code again just because they are in different languages. 存在绑定时,您不必再次编写所有代码,因为它们使用不同的语言。 Another motivation for bindings is that you can benefit from the advantages of different programming languages. 绑定的另一个动机是您可以从不同编程语言的优势中受益。 For example, C/C++ are much faster than JavaScript. 例如,C / C ++比JavaScript快得多。 It might be beneficial to write some code in C/C++ for performance purposes. 为了性能目的,在C / C ++中编写一些代码可能是有益的。

Now let's take a look at the picture you attached. 现在让我们来看看你附上的图片。 V8 engine, according to Google Official website, is " written in C++ ". 据谷歌官方网站称,V8引擎是“ 用C ++编写的 ”。 libuv adds a layer of abstraction that provides asynchronous I/O operations, written in C. However, the core functionalities of Node.js, such as networking, Database queries, file system I/O, are provided in libraries (or modules if you prefer) that are written in JavaScript. libuv添加了一个提供异步I / O操作的抽象层,用C语言编写。然而,Node.js的核心功能,如网络,数据库查询,文件系统I / O,都在库中提供(或模块,如果你更喜欢)用JavaScript编写。 Plus, your code is written in JavaScript as well. 另外,您的代码也是用JavaScript编写的。 Now in order for these pieces of technology written in different programming languages to communicate with each other, you have to "bind" them together, using bindings. 现在,为了使用不同编程语言编写的这些技术相互通信,您必须使用绑定将它们“绑定”在一起。 These bindings are node.js bindings. 这些绑定是node.js绑定。

I've written an article lately that explains the architecture of Node.js' internal codebase where I explained how binds fit into Node.js! 我最近写了一篇文章解释了Node.js内部代码库的架构,在那里我解释了绑定如何适应Node.js!

Upon further research i've come across this article. 经过进一步的研究,我发现了这篇文章。 I hope this helps anyone out: 我希望这可以帮助任何人:

http://pravinchavan.wordpress.com/2013/11/08/c-binding-with-node-js/ http://pravinchavan.wordpress.com/2013/11/08/c-binding-with-node-js/

Node.js bindings are series of methods that can be used in Node.js code which are in reality just running C++ code behind the scenes. Node.js绑定是可以在Node.js代码中使用的一系列方法,这些代码实际上只是在幕后运行C ++代码。

fs.readFile()  

This method is not part of javascript. 此方法不是javascript的一部分。 It's provided to v8 as part of the node.js runtime. 它作为node.js运行时的一部分提供给v8。 So javascript does not know how to read a file from disk but C++ does. 所以javascript不知道如何从磁盘读取文件,但C ++确实如此。 So when we use javascript code and node.js to read a file from disk it just defers all of that to the C++ function that can actually read the file from disk and get the results back. 因此,当我们使用javascript代码和node.js从磁盘读取文件时,它只是将所有这些推迟到C ++函数,该函数实际上可以从磁盘读取文件并获得结果。

在此输入图像描述

Javascript also has bindings in the browser too. Javascript在浏览器中也有绑定。 for example; 例如;

document.querySelector()

is not a javascript code. 不是javascript代码。 It is implemented by chrome V8 engine. 它由chrome V8引擎实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM